In Non-Lexical Lifetimes: Introduction, Niko includes the following snippet:
fn get_default3<'m,K,V:Default>(map: &'m mut HashMap<K,V>, key: K) -> &'m mut V { map.entry(key) .or_insert_with(|| V::default()) }
What does the || V::default()
mean here?
|| Ok(HelloWorld) is a closure that takes 0 arguments and returns Result<R, hyper::Error> .
“Pipes” are used to defined closures https://doc.rust-lang.org/book/second-edition/ch13-01-closures.html. _ is used when there is an input argument that you simple don't use and the way to tell the compiler about it is to add a _ you can also do _my_var as well for the same result.
It is a closure with zero arguments. This is a simplified example to show the basic syntax and usage (play):
fn main() { let c = || println!("c called"); c(); c(); }
This prints:
c called c called
Another example from the documentation:
let plus_one = |x: i32| x + 1; assert_eq!(2, plus_one(1));
It's a zero-argument lambda function.
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