A String in Rust is a growable, mutable, UTF-8 encoded text string. It is implemented as a Vec<u8> with some additional logic to ensure that the data is always valid UTF-8. You can also use the +
operator or the format!
macro to concatenate String values.
Appending to a String with push_str and push
The push_str
and push
methods on the String
struct in Rust are used to append data to the end of a string. The push_str
method takes a string slice as a parameter, while the push
method takes a single character as a parameter.
Here is an example of how to use the push_str
method to append a string slice to a string:
let mut s = String::from("foo");
s.push_str("bar");
println!("s is {}", s);
Output:
s is foobar
Here is an example of how to use the push
method to append a single character to a string:
let mut s = String::from("lo");
s.push('l');
println!("s is {}", s);
Output:
s is lol
It is important to note that the push_str
method does not take ownership of the string slice that it is passed. This means that the string slice can still be used after it is appended to the string.
Here is an example of how to use the push_str
method to append the contents of one string to another string:
let mut s1 = String::from("foo");
let s2 = "bar";
s1.push_str(s2);
println!("s1 is {}", s1);
println!("s2 is {}", s2);
Output:
s1 is foobar
s2 is bar
As you can see, the push_str
method has appended the contents of the s2
string to the s1
string. However, the s2
string is still available and can still be used.
The push_str
and push
methods are both very useful for appending data to strings in Rust. The push_str
method is especially useful when you want to append the contents of another string to a string.
Concatenation with the + Operator or the format! Macro
Concatenation with the + operator
The + operator can be used to concatenate two strings, but only if one of the strings is a &str (string slice). If both strings are String, the compiler will error. This is because the add method that is called when we use the + operator takes ownership of its self parameter, but not its s parameter. This means that the String that is passed to the add method will be moved and will no longer be valid after the operation.
If we need to concatenate multiple strings using the + operator, we can do so by using temporary variables:
let s1 = String::from("Hello, ");
let s2 = String::from("world!");
let s3 = String::from("!");
let mut s = s1;
s = s + &s2;
s = s + &s3;
This code will create a new String object for each concatenation operation, which can be inefficient if we are concatenating many strings.
Concatenation with the format! macro
The format! macro provides a more efficient way to concatenate multiple strings. It takes a template string as its first argument, and then any number of expressions as its subsequent arguments. The expressions are evaluated and substituted into the template string, and the result is returned as a new String object.
The format! macro is more efficient than using the + operator to concatenate multiple strings because it does not need to create a new String object for each concatenation operation. Instead, it reuses the same String object and simply appends new data to it.
Example
The following code shows how to use the format! macro to concatenate multiple strings:
let s1 = String::from("Hello, ");
let s2 = String::from("world!");
let s3 = String::from("!");
let s = format!("{s1}{s2}{s3}");
This code will create a new String object with the value "Hello, world!".
Which method to use?
In general, it is best to use the format! macro to concatenate multiple strings. It is more efficient and easier to read than using the + operator. However, there are some cases where it may be necessary to use the + operator, such as when concatenating a string with a non-string type.