3 ms·
This is awesome. How did you figure this out? The documentation on `split` says: "The pattern can be a &str, char, or a closure that determines the split." Bu
by terhechte 8y ago
This is awesome. How did you figure this out? The documentation on `split` says:
"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.
- gridspy 8y agoVec<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-slices https://doc.rust-lang.org/book/ch04-03-slices.html#string-sl... https://doc.rust-lang.org/std/slice/ https://doc.rust-lang.org/std/slice/
- steveklabnik 8y ago&str is not the same as &[char]. &str has the same memory representation as &[u8]. char is four bytes, not one.
- gridspy 8y agoThanks Steve.
- steveklabnik 8y agoNo problem! It's an easy mistake to make.
- steveklabnik 8y agoWould 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!
- terhechte 8y agoYeah, will do
- steveklabnik 8y agoAwesome, thank you!
- general_pizza 8y agoThe 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#implementors https://doc.rust-lang.org/std/str/pattern/trait.Pattern.html...