Issue
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?
Solution
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));
Answered By - Arjan Answer Checked By - Mildred Charles (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.