I study the std::ops::Add, and etc, and I wanna know whether rust have such syntactic salt which mention as below,
I check the std::cmp::PartialEq, PartialOrd, not support different types comparison,
anything else can make it as below as expectation?
fn main() {
let a = [1,2,3,4,5];
println!("{:?}", a>3); //expect the result: [false, false,false, true, true]
}
alice
2
No, Rust does not provide this kind of thing.
However, when you have an array, you can use array::map like this:
fn main() {
let a = [1,2,3,4,5];
println!("{:?}", a.map(|val| val > 3));
}
If it's a vector rather than an array, you would go through iterators:
fn main() {
let a = vec![1,2,3,4,5];
println!("{:?}", a.into_iter().map(|val| val > 3).collect::<Vec<_>>());
}
4 Likes
system
Closed
4
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.