Re:Learning Rust via Advent of Code
by general_pizza on Monday February 04, 2019 @05:56PM
(#19081518)
The docs are a tad misleading but the important piece is to look at the signature. The `where P: Pattern<'a>` portion states that split needs a type that implements the Pattern trait. If you follow the link to the Pattern trait then at the bottom you'll see a section[0] listing types implementing the trait (&[char] being one) and therefore can be passed into split. Hope that helps!
[0] https://doc.rust-lang.org/std/str/pattern/trait.Pattern.html...
Re:Learning Rust via Advent of Code
by steveklabnik on Monday February 04, 2019 @05:32PM
(#19081311)
Would you be willing to file a docs bug so we can clarify this?
It’s possible that it used to be only those types, but was expanded. We should fix this!
Yeah, will do
Re:Learning Rust via Advent of Code
by steveklabnik on Tuesday February 05, 2019 @03:14AM
(#19083709)
Awesome, thank you!
Vec<char> or an array for instance - [char; 20] can (automatically) create a "slice" of type &[char]. This is true for all types (not just char).
The operator for this is for eg l[1..10]. It can also happen automatically.
&str is the same type as &[char] - just a renaming.
Still learning Rust myself, apologies if this leads you astray. Just do some reading on slices.
https://doc.rust-lang.org/book/ch04-03-slices.html#string-sl...
Re:Learning Rust via Advent of Code
by steveklabnik on Monday February 04, 2019 @05:30PM
(#19081298)
&str is not the same as &[char]. &str has the same memory representation as &[u8]. char is four bytes, not one.
Thanks Steve.
Re:Learning Rust via Advent of Code
by steveklabnik on Tuesday February 05, 2019 @03:14AM
(#19083707)
No problem! It's an easy mistake to make.
"The pattern can be a &str, char, or a closure that determines the split."
But in your case it is an array of chars and it splits for each of them. I don't see this documented at all.