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
.split_strreturns an iterator over&strslices, that is, it is returning subviews of therowdata. The borrowed&stris not an owned~str: to make this work, either collect to a~[&str], or, copy each&strinto a~strbefore collecting:FWIW, if you're splitting on a single-character predicate, then
splitwill 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.10to0.11ormasterin the above documentation links for the corresponding docs).With the nightly, the two snippets above would be written as:
(Lastly, if you're struggling with the distinction of
&strvs.~strakaString, I wrote up some details a while ago.)