rust使用map filter筛选内容相当的方法,下面放代码给大家研究一下。
第一种:map与filter的结合
let myv:Vec<String>=vec!["hello".to_string(),"castlepeak".to_string(),"他大舅".to_string()];
let myvec1:Vec<String>=myv.into_iter().map(|x|x).filter(|x|x.contains("hello")).collect();
println!("经过map后再filter的结果:{:?}",myvec1);
输出结果:
经过map后再filter的结果:["hello"]
第二种:单独使用filter
let myv:Vec<String>=vec!["hello".to_string(),"castlepeak".to_string(),"他大舅".to_string()];
let myvec2:Vec<String>=myv.into_iter().filter(|x|x.contains("peak")).collect();
println!("经过filter的结果:{:?}",myvec2);
输出结果:
经过map后再filter的结果:["castlepeak"]
知识拓展:
上面的map filter,还可以自己再再条件再筛选,比如:
let myvec1:Vec<String>=myv.into_iter().map(|x|x).filter(|x|! x.contains("hello")).filter(|x| x.contains("大")).collect();
其它的请自行研究了。告辞。