Hacker News new | past | comments | ask | show | jobs | submit login

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.




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...


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


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...

https://doc.rust-lang.org/std/slice/


&str is not the same as &[char]. &str has the same memory representation as &[u8]. char is four bytes, not one.


Thanks Steve.


No problem! It's an easy mistake to make.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: