How to join a vector of strings in Rust? Analogue of join() from other languages
Hello everyone! I just started learning Rust, and I have a seemingly simple task: I have a vector of strings `Vec`, and I want to join them into a single string with a separator (e.g., comma). In other languages like Python or JavaScript, I would write something like `",".join(my_list)` or `my_list.join(",")`. But in Rust, I'm confused.
Here's what I'm trying, but it doesn't work:
```rust
let words = vec!["hello".to_string(), "world".to_string(), "rust".to_string()];
let joined = words.join(","); // Error! No such method?
let joined2 = ", ".join(&words); // Also doesn't compile
```
I've already read that Rust has a `Join` trait, but I don't understand how to use it correctly. I tried googling, but I only find examples with `&str`.
I need the result to be a
No answers yet.
Log in to leave an answer
Blogs