【Rust】trait-实现
环境
- Rust 1.56.1
- VSCode 1.61.2
概念
参考:https://doc.rust-lang.org/rust-by-example/trait/impl_trait.html
示例
静态返回
fn combine_vecs(v: Vec<i32>, u: Vec<i32>) -> impl Iterator<Item = i32> {
v.into_iter().chain(u.into_iter()).cycle()
}
fn main() {
let v1 = vec![1, 2, 3];
let v2 = vec![4, 5];
let mut v3 = combine_vecs(v1, v2);
assert_eq!(Some(1), v3.next());
assert_eq!(Some(2), v3.next());
assert_eq!(Some(3), v3.next());
assert_eq!(Some(4), v3.next());
assert_eq!(Some(5), v3.next());
println!("all done");
}
返回闭包
fn make_adder_function(y: i32) -> impl Fn(i32) -> i32 {
move |x| x + y
}
fn main() {
let plus_one = make_adder_function(1);
assert_eq!(plus_one(2), 3);
}
返回迭代器
fn double_positives<'a>(numbers: &'a Vec<i32>) -> impl Iterator<Item = i32> + 'a {
numbers.iter().filter(|x| x > &&0).map(|x| x * 2)
}
fn main() {
double_positives(&vec![1, 2]).count();
}
总结
了解了 Rust 中 impl trait 的使用方法。