Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@IanWhitney
Created February 23, 2016 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IanWhitney/934c451b15ff21478837 to your computer and use it in GitHub Desktop.
Save IanWhitney/934c451b15ff21478837 to your computer and use it in GitHub Desktop.

My initial confusion started when I looked at this code:

http://exercism.io/submissions/e3e0acc3148b4f42aed494485ccd0bb8

Specifically this bit

.collect::<String>() {
        ref s if s.is_empty() => return num.to_string(),
        s => s,
    }

This syntax of having a scope (?) after the string function was new to me. So I checked out the documentation of collect to see if there were examples. Becuase to me (as a rubyist) this looks like we're passing a block to collect. Though that's probably not what's going on.

Anyway, the collect documentation doesn't show any examples like this. But the iterator trait list does show this weird (to me) {...} syntax after each function

 fn collect<B>(self) -> B where B: FromIterator<Self::Item> { ... }

So I figured that the syntax that confused me and this { ... } were related. But I couldn't find any documentation about what is going on here.

So...what's going on here. And is there a place in the book where we could document this sort of thing?

Related, what is the ref keyword in the code sample? That is something else I looked for in the book and the API documentation and had no luck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment