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

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -