Adding a symbol (or group symbols) to each Vector element on Rust

What is my problem?
I get the current directory with the help

let dir: String = env::current_dir().unwrap().display().to_string();

Next, I need to make Vec<String> out of this line, for example, so that from a string type "home/user/project" to get vec!["home/", "user/", "project"] as String.

I tried a couple of options, but there are constantly errors of types and much more, but one of my options, which, in my opinion, was closest to

let dir_split: Vec<(there should be a type of String, but it turns out ())> = dir.split('/').map(|s| s.to_string().push('/')).collect();

>Solution :

Instead of str::split use str::split_inclusive which would mean you don’t need to manually push, which is causing you issues.

When you use map, you are mapping each value to the the value returned by the expression. In this case, this would be String::push, which is returns ().

let var: Vec<_> = dir.split_inclusive('/')
    .map(String::from)
    .collect();

Leave a Reply