What makes rust unary || (parallel pipe) mean?

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 || V::default() || V::default() here?

+7
rust
source share
2 answers

This is a closure with null arguments. This is a simplified example showing the basic syntax and usage ( play ):

 fn main() { let c = || println!("c called"); c(); c(); } 

Fingerprints:

 c called c called 

Another example from the documentation :

 let plus_one = |x: i32| x + 1; assert_eq!(2, plus_one(1)); 
+16
source share

This is a lambda function with a null argument.

+6
source share

All Articles