在Struct Vec中查找Struct的惯用方法,然后在Rust中对该Struct执行Trait函数



在我的项目中,我经常通过结构体向量迭代以通过一些字段值找到对象,然后在该对象上使用一些trait函数:

pub struct Person{
name: String,
age: u32,
id: u32,
}
impl Person{
pub fn new(name: String, id_num: u32, age: u32)->Self{
let p = Person{
name: name,
id: id_num,
age: age,
};
p
}
}
trait PersonTrait{
fn printname();
fn get_name()->String; 
fn get_age()->u32;
fn set_age(age: u32);
}

impl PersonTrait for Person{
fn printname(){
dbg!(self.name)
}
fn get_name()->String{
self.name
}
fn get_id()->u32{
self.id;
}
fn set_age(age: u32){
self.age = age;
}
}

fn main(){
let my_people = vec![Person::new("Rakim".to_string(), 1, 56), Person::new("Ghostface".to_string(), 2, 56), Person::new("RZA".to_string(), 3, 56)];
//frequently repeating this pattern of finding struct in array of structs, then doing something to that found struct
for person in my_people.clone(){
if person.get_id() == 1 {
person.set_age(100);
}
}
for person in my_people.clone(){
if person.get_id() == "Rakim".to_string(){
person.printname();
}
}

}
所以我在这里使用的一般模式是:
for x in my_objects{
if x.id() == some_id{
x.do_some_trait_function()
}
}
我想创建一个更通用的函数来简化语法,比如:
//not sure what the correct syntax would be here, or how you might pass a trait function as an argument
fn find_then_do_trait_function(obj_list: Vec<Person>, id: u32, trait_function: my_trait_function()){ 
for x in obj_list(){
if x.get_id() == id {
//use my trait function on x
}
}
}

我该怎么做呢?我知道我可以为每个trait函数创建一个enum,然后为枚举创建一个match,但这看起来也很冗长。

trait函数没有什么独特之处。您已经确定了一个非常常见的模式,它可以分为两部分:我们想要过滤一个向量,然后对每个匹配的元素执行一些操作。我们可以定义一个带两个闭包参数的函数来完成此操作。

fn search_and_call<T>(obj_list: &mut Vec<T>,
mut condition: impl FnMut(&mut T) -> bool,
mut func: impl FnMut(&mut T) -> ()) {
for x in obj_list {
if condition(x) {
func(x);
}
}
}

func可以是任意闭包。闭包可以调用trait函数,或者它可以打印到屏幕上,或者做任何事情。上面函数的作者不需要关心;对我们来说都是一样的。示例用法:

let mut v = vec!(1, 2, 3, 4);
search_and_call(&mut v, |x| *x % 2 == 0, |x| println!("{}", *x));

值得注意的是,Rust优秀的Iterator特性定义了大量有用的函数,我们可以免费获得这种行为,甚至不需要接触for循环。

let mut v = vec!(1, 2, 3, 4);
v.iter().filter(|x| *x % 2 == 0).for_each(|x| println!("{}", *x));

.iter()在vector上得到一个迭代器,.filter(...)产生一个新的迭代器,根据条件选择特定的元素,.for_each(...)对过滤器后剩下的所有元素调用一个函数。

最新更新