I have a vector of Option
s and I want to filter only the Some
s. I use filter_map
with identity:
let v = vec![Some(1), None, Some(2)];
for i in v.into_iter().filter_map(|o| o) {
println!("{}", i);
}
Is there a builtin function permitting to write something like filter_map(identity)
?
Hence f:R→R,f(x)=x is an identity function.
Properties of Identity FunctionIn topological space, this function is always continuous.
Overview. The identity() function from numpy library is used to return the identity matrix. Note: An identity matrix is the type of matrix in which all the elements on the main diagonal are ones (1) and all other elements are zeros (0).
After Rust 1.33, see the sibling answer.
Before Rust 1.33, there is no such function in stable Rust. You can create your own:
fn id<T>(v: T) -> T { v }
Although most people just inline the code, as you did.
After Rust 1.29, use Iterator::flatten
:
let v = vec![Some(1), None, Some(2)];
for i in v.into_iter().flatten() {
println!("{}", i);
}
Before Rust 1.29, use Iterator::flat_map
:
let v = vec![Some(1), None, Some(2)];
for i in v.into_iter().flat_map(|o| o) {
println!("{}", i);
}
See also:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With