4

I am trying to collect into an vec of strings in rust using the following:

let fields : ~[~str] = row.split_str(",").collect();

I get the following error: expected std::iter::FromIterator<&str>, but found std::iter::FromIterator<~str> (str storage differs: expected & but found ~)

I have tried to use type hints but with no success

2
  • What version of Rust are you using? Commented Jul 11, 2014 at 3:34
  • 2
    The tilde syntax was removed from the language -- replaced by the Box. If you are not a computer historian, I recommend skipping this question completely. This one is similar but newer: stackoverflow.com/questions/62690352/… Commented Feb 14, 2022 at 5:56

1 Answer 1

10

.split_str returns an iterator over &str slices, that is, it is returning subviews of the row data. The borrowed &str is not an owned ~str: to make this work, either collect to a ~[&str], or, copy each &str into a ~str before collecting:

let first: ~[&str] = row.split_str(",").collect();
let second: ~[~str] = row.split_str(",").map(|s| s.to_owned()).collect();

FWIW, if you're splitting on a single-character predicate, then split will be more efficient, (e.g. row.split(',') in this case).

Also, I recommend you upgrade to a more recent version of Rust, 0.11 was recently released, but the nightlies are the recommended install targets (change the 0.10 to 0.11 or master in the above documentation links for the corresponding docs).

With the nightly, the two snippets above would be written as:

let first: Vec<&str> = row.split(',').collect();
let second: Vec<String> = row.split(',').map(|s| s.to_string()).collect();

(Lastly, if you're struggling with the distinction of &str vs. ~str aka String, I wrote up some details a while ago.)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! And yes I am now upgrading and changing my syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.