What does Rust's unary || (parallel pipe) mean?

后端 未结 2 1094
慢半拍i
慢半拍i 2021-01-03 23:06

In Non-Lexical Lifetimes: Introduction, Niko includes the following snippet:

fn get_default3<\'m,K,V:Default>(map: &\'m mut HashMap,
            


        
相关标签:
2条回答
  • 2021-01-03 23:40

    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));
    
    0 讨论(0)
  • 2021-01-04 00:03

    It's a zero-argument lambda function.

    0 讨论(0)
提交回复
热议问题