Rust Lang

Rust Append to String

Strings are essential part of many programs and there are several ways to manipulate and combine them in Rust. In this tutorial, we’ll explore how to append the strings to create new ones which can be helpful in various scenarios such as building dynamic user interfaces, constructing log messages, or generating the output for a program.

Method 1: Using the Addition (+) Operator

The most common method that we can use to append the strings in Rust is using the addition (+) operator. The addition operator allows us to concatenate two or more strings into a new string value.

It is good to keep in mind that the addition (+) operator takes ownership of its operands. Hence, we can no longer use the first string after the operation.

Consider the following example code that demonstrates how to use this operator to append two strings:

fn main() {
    let s1 = String::from("hello");
    let s2 = " world";

    let s4 = s1 + s2;
    println!("{}", s4);
}

 
In the previous example, we use the “+” operator to concatenate two strings and produce a new string as defined in the variable s4.

We can then run the previous code and print the new string as shown in the following:

Compiling rust_json v0.1.0 (.\Rust\append)
Finished dev [unoptimized + debuginfo] target(s) in 0.27s
Running `target\debug\append.exe`

 
Resulting String:

hello world

 

Method 2: Using the Push_Str Method

Another standard method to append the strings in Rust is the “push_str” method. This method is part of the Rust standard library and allows us to append a string slice to the end of an existing string value.

The method definition is as shown int the following:

fn push_str(&mut self, string: &str)

 
This method takes a mutable reference to a string instance (&mut self) and a string slice (&str) as its arguments.

The “&mut self” parameter allows the method to modify the original string value by appending the string slice to the end of it.

NOTE: The “push_str” method does not allocate a new memory to create the resulting string value. Instead, it modifies the existing string value which makes it a very efficient method of appending the string slices.

It is also recommended that the “&str” parameter be a string slice instead of a string value. This is because a string value is a heavier owned string, and passing it to the “push_str” method requires a copy or moving the string’s data. This can be an expensive operation depending on the task at hand.

The following example demonstrates how we can use the “push_str” method to concatenate two string literals:

fn main() {
    let mut s = String::from("hello");
    s.push_str(", world");
    println!("{}", s);
}

 
This should return the output string as follows:

hello, world

 

Method 3: Using the Format! Macro

We can also use the format! macro in Rust to append two strings. An example code is as demonstrated in the following:

fn main() {
    let s1 = String::from("hello");
    let s2: String = String::from(" world!");

    let s3 = format!("{}{}", s1, s2);
    println!("{}", s3);
}

 
In the provided example, the format! macro creates a new string that contains the concatenated values of s1 and s2. The resulting string is stored in s3, while s1 and s2 remain unchanged.

Conclusion

We explored how to use the three Rust features to append the strings. We explored how to work with the addition operator,  the push_str method, and the format! macro.

Each method has advantages and disadvantages, depending on the specific use case. By understanding the different techniques and their trade-offs, you can choose the best approach for your program.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list