Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading