3 ms·
How does that one work?
by Lvl999Noob 5y ago
How does that one work?
- steveklabnik 5y agofn punch_card() -> impl std::fmt::Debug { This is a function that returns some type that implements Debug. Not specifically named. The rest of it is a combination of two syntaxes: .. and ..= These are both ranges. The former exclusive, the latter inclusive. You can write something like 1..5 (or 1..=5) to get a range from 1 to 5 inclusive/exclusive, but you can also leave the start or end off: 2.. will give you from 1 till the end, ..=5 will give you from the start to 4. This means that .. will give you the full range. It looks more normal when used in context: fn main() { let s = "some string"; let some = &s[..=4]; // you'd probably write ..5 but i wanted to show both syntaxes let string = &s[5..]; let all = &s[..]; // a no-op here but useful if s were a String and you wanted 'all' to be a &str println!("{}", some); println!("{}", string); } prints "some" and then "string". So, here's the tricky bit: you can overload what the range is over. They're not just for numbers. So: fn main() { let mut string = 'a'..='z'; let a = string.next(); let b = string.next(); println!("{:?}", a); println!("{:?}", b); } prints "Some('a')" and then "Some('b')", because iterators return Options. So... all of this means that, you can implement a range of ranges. And so this string builds up a ridiculous unbounded range of unbounded range of unbounded range of ...