3

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

tshepang
  • 12,111
  • 21
  • 91
  • 136
rdmcfee
  • 540
  • 6
  • 13
  • What version of Rust are you using? – huon Jul 11 '14 at 03: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: https://stackoverflow.com/questions/62690352/how-to-return-a-vector-of-strings-in-rust – David Lotts Feb 14 '22 at 05:56

1 Answers1

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.)

Community
  • 1
  • 1
huon
  • 94,605
  • 21
  • 231
  • 225