How to split a string (String or &str) into parts in Rust?
Hi everyone! I just started learning Rust and encountered a task that in other languages (e.g., Python or JavaScript) is solved in one line. I need to split a string by a delimiter, such as a space or comma, and get an array or vector with the parts.
Here's what I'm trying to do:
```rust
let text = "apple,banana,grape";
// I want to get ["apple", "banana", "grape"]
```
I tried using the `.split()` method, but it returns some kind of iterator, not an array. When I try to collect the result into a vector using `.collect()`, the compiler complains about types. Also, I don't understand the difference between `String` and `&str` when splitting — do they have different methods? And what should I do if the string contains Unicode characters (e.g., Cyrillic)? Will `.split()` work correctly?
Please advise.
No answers yet.
Log in to leave an answer