I can't understand how Rust works with strings. I created a simple struct with two string fields and one method. This method concatenates both fields and the string from arguments. My code:
fn main() {
let obj = MyStruct {
field_1: "first".to_string(),
field_2: "second".to_string(),
};
let data = obj.get_data("myWord");
println!("{}",data);
}
struct MyStruct {
field_1: String,
field_2: String,
}
impl MyStruct {
fn get_data<'a>(&'a self, word: &'a str) -> &'a str {
let sx = &self.field_1 + &self.field_2 + word;
&* sx
}
}
I get an error when run it:
src\main.rs:18:18: 18:31 error: binary operation `+` cannot be applied to type `&collections::string::String` [E0369]
src\main.rs:18 let sx = &self.field_1 + &self.field_2 + word;
^~~~~~~~~~~~~
src\main.rs:19:10: 19:14 error: the type of this value must be known in this context
src\main.rs:19 &* sx
^~~~
error: aborting due to 2 previous errors
Could not compile `test`.
To learn more, run the command again with --verbose.
I read this chapter from the Rust book. I try to concatenate strings like in code example, but the compiler says that it's not a string.
I searched online, but there were no examples for Rust 1.3.