rust - How can I concatenate something to the front of a string? -
i keep getting error "use of moved value".
let mut s = "s".to_string(); s = s + &s;
think + desugars to:
s = s.add(&s); now add from add trait , this:
fn add(self: string, rhs: &str) -> string; the use of self means taking ownership of string; can’t pass reference second argument, because isn’t yours more.
you might think it’d ok doing this, it’s not; whole class unsound, constituting mutable aliasing—both mutable , immutable reference same thing existing @ same time. specific case, 1 way imagine going wrong if permitted if string pushing reallocate string; rhs conceivably pointing non-existent memory when went use it.
Comments
Post a Comment