Closed
Description
Example I came up with (tried to reduce from a larger set of code I found this in)
fn main() {
let mut v = vec!["hello", "this", "is", "a", "test"];
let v2 = Vec::new();
v.into_iter().map(|s|s.to_owned()).collect::<Vec<_>>();
let mut a = String::new();
for i in v {
a = *i.to_string();
v2.push(a);
}
}
This gives a rather confusing error message:
error[E0308]: mismatched types
--> src/main.rs:10:7
|
10 | a = *i.to_string();
| ^^^^^^^^^^^^^^
| |
| expected struct `std::string::String`, found str
| help: try using a conversion method: `*i.to_string().to_string()`
|
= note: expected type `std::string::String`
found type `str`
meta:
rustc 1.30.0-nightly (d5a448b3f 2018-08-13)
binary: rustc
commit-hash: d5a448b3f47b22c9cb010198bdcc49d4392b090b
commit-date: 2018-08-13
host: x86_64-unknown-linux-gnu
release: 1.30.0-nightly
LLVM version: 7.0
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
matthew-russo commentedon Aug 15, 2018
Not sure if this is entirely correct but I'm pretty sure it has to do with precedence of the dereference.
This code has the same issue:
while if you write:
it will compile fine.
@matthiaskrgr Also just to note there were some other issues with the code you posted --
v2
was not mutable so couldn't be pushed to and thev.into_iter()...
movedv
so the for loop was then invalid due to moved valuematthew-russo commentedon Aug 15, 2018
And also just wanted to point out that this is a redundant operation to begin with because you're trying to convert a
String
to aString
.This is an equivalent example with strong typing on v2 to see that its still
Vec<String>
.or if you still wanted to iterate over v (ignoring the move) you could do:
matthiaskrgr commentedon Aug 15, 2018
Yeah, I know the code is quite wonky, :) I was in the middle of a refactoring when I stumbled over the .to_string().to_string() oddity.
Rollup merge of rust-lang#53406 - estebank:to_string-to_string, r=mic…
ishowta commentedon Oct 1, 2020
This problem looks essentially the same as #64919 and that has been resolved.
So I think #53406 should be reverted.
It's confusing if there are no hints.