Parameters without separation

Can anyone help me understand why the Map parameters don’t have a separator (such as a comma or colon)?

Example:
let maybe_some_len = maybe_some_string.map(|s| s.len());

The documentation doesn’t appear to suggest this style in any way. Am I missing something?

map only takes 1 parameter other than its reciever. You could write it as

let maybe_some_len = Option::map(maybe_some_string, |s| s.len());

But that is the same as the method syntax.


Btw, you can format your code like this

```rust
// your code here
```
1 Like

I like your approach as a function rather than a method.

Can you walk me through the 1 parameter? What is the purpose of the vertical bars with relationship to a String?

Ah, I see. The vertical bars signify the start of a closure, (in other languages they are called anonymous functions, arrow functinons, etc.)

You can read about them here, as the book does a fantastic job of explaining them
https://doc.rust-lang.org/book/ch13-01-closures.html
https://doc.rust-lang.org/rust-by-example/fn/closures.html

And if you want to see how they work, you can ready my blog about them, although I should warn you, I do go in deep into how closures work, if you are unfamiliar with trait, generics, and how they work together I would put this off for now until you get familiar with them.

If you have any questions, feel free to ask here or message me!

2 Likes

Thanks Krishna,

This is great to know, I am currently on chapter 8 of The Book. I have not yet reached traits. For now, I am satisfied to at least know that I am looking at a closure. I will temporarily hold off learning about closures and continue to read the book sequentially.

Coming from Python, I have been introduced to many new concepts. It appears I went down a worm hole, thanks for your patience.

1 Like

No problem! Have fun learning Rust!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.