String append, cannot go from dereferencing the & & pointer

I'm having trouble concatenating the two lines, I'm very new to rust. If there is an easier way to do this, please feel free to show me.

My function goes through a vector of string tuples (String,String), what I want to do is combine these two string elements into one string. Here is what I have:

for tup in bmp.bitmap_picture.mut_iter() {
    let &(ref x, ref y) = tup;
    let res_string = x;
    res_string.append(y.as_slice());
}

but I get an error: error: cannot move out of dereference of '&'-pointerfor the line:res_string.append(y.as_slice());

I also tried res_string.append(y.clone().as_slice());, but the same error occurred, so I'm not sure if that was even correct.

+4
source share
2 answers

Append function definition:

fn append(self, second: &str) -> String

self -. , ( ). , x, y.

, move_iter.

:

let string_pairs = vec![("Foo".to_string(),"Bar".to_string())];

// Option 1: leave original vector intact
let mut strings = Vec::new();
for &(ref x, ref y) in string_pairs.iter() {
    let string = x.clone().append(y.as_slice());
    strings.push(string);
}

// Option 2: consume original vector
let strings: Vec<String> = string_pairs.move_iter()
    .map(|(x, y)| x.append(y.as_slice()))
    .collect();
+9

, append, push_str, ( ), , , . , - append push_str. "ref x" "ref mut x", .

+4

All Articles