PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, December 12, 2022

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

 December 12, 2022     rust, syntax     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing