-1

While going through rust documentation I found below code

let k = 21;
let x : Result<_, &str> = Ok("foo");
assert_eq!(x.map_or_else(|e| k * 2, |v| v.len()), 3);

What exactly is |e| & |v| in last line ? why it is required?

kmdreko
  • 42,554
  • 6
  • 57
  • 106
rakd
  • 179
  • 1
  • 2
  • 12
  • It's a Closure. See the [docs here](https://doc.rust-lang.org/book/ch13-01-closures.html) – Abdul Niyas P M Jan 31 '22 at 11:14
  • https://doc.rust-lang.org/rust-by-example/fn/closures.html – CherryDT Jan 31 '22 at 11:14
  • Relevant: https://stackoverflow.com/q/25182565 https://stackoverflow.com/q/36988622 – E_net4 Jan 31 '22 at 11:28
  • @user4815162342 I agree with why this would have been downvoted . But before posting it here I did try searching "what is ` ||` in rust ?",but unfortunately no topmost results linked me to "rust closures". Now I realized that I should have searched something like "what is pipe symbol in rust?" instead of just "what is || in rust?" ,that should have solved my problem . People like me who don't know what to call `|` symbol as will surely get confuse what is `||` in rust. – rakd Jan 31 '22 at 14:27
  • 1
    @divakar I have sympathy for your position, which is why I personally refrained from downvoting. I guess StackOverflow is just not a good venue for issues like that; fortunately there is also a [user forum](https://users.rust-lang.org/) and even [reddit](https://www.reddit.com/r/learnrust/) where questions like this one are appropriate and welcome. – user4815162342 Jan 31 '22 at 14:33
  • 1
    @divakar I am one of the downvoters, and while I agree that other people may find it an appropriate question, I do consider trying to learn a language using SO, or even Google at least for Rust, as an insufficient research effort. Especially when the official book is really well-written, and you can find alternative resources, e.g. rust-by-example. The syntactical constructs of a language may be very hard to search in Google or SO (usually because of many symbols), but are mentioned in every tutorial. – Chayim Friedman Feb 01 '22 at 13:53
  • @divakar I would _not_ vote down e.g. a question about borrow-checking, even if it _is_ stated in the book, because I understand that grasping the concept may be quite hard. But syntactical constructs vary from one language to another (btw, closure syntax in Rust is taken from Ruby), while staying the same idea mostly. – Chayim Friedman Feb 01 '22 at 13:55
  • @divakar [Is it possible to learn a new language through Stack Overflow?](https://meta.stackexchange.com/questions/3131/is-it-possible-to-learn-a-new-language-through-stack-overflow) may be relevant here. – Chayim Friedman Feb 01 '22 at 13:55

1 Answers1

4

The .map_or_else functions take a closure as a parameter. A closure is an anonymous function that can optionally capture the environment where they are defined. The syntax to define a closure is

let c = |x| println!(x + 2);

This takes 1 parameters which is mapped to the x variable. Calling c(2) will print 4, calling c(4) will print 6.

Similarly |e| k * 2 and |v| v.len() is also a closure. They also take 1 parameter, e and k which are the parameter names whose values will be filled by the .map_or_else function.

user4815162342
  • 141,790
  • 18
  • 296
  • 355
Arijit Dey
  • 316
  • 2
  • 10