This article will explore how to perform string concatenation in the Rust programming language.
The + Operator
In other programming languages, you may use the + operator to join strings. Rust also offers this method of string concatenation as shown below:
let str1 = "hello".to_string();
let str2 = " world".to_string();
println!("{}", str1 + &str2);
}
In the previous example, we use the to_string method to convert the specified string to a String type.
Then, we use the + operator to concatenate the strings. Note that we pass the second string as a string slice.
Once we run the program above, we should get an output as:
hello world
Concat! Macro
Rust provides us with the concat!() macro. This macro concatenates two strings into a static string. An example is as shown below:
let string = concat!("hello", " ", "world");
println!("{}", string);
}
The concat!() macro takes the comma-separated strings as the arguments and combines them into a static string.
The previous code should return an output as:
hello world
Push_str() Method
You can also use the push_str method to concatenate strings. The push_str method will modify the original string. Hence, the original string should be a mutable type, as shown in the example below:
let mut str1 = “hello”.to_string();
let str2 = “ world”.to_string();
str1.push_str(&str2);
println!(“{}”, str1);
}
The previous code should concatenate the provided strings. Remember that the push_str method requires the original string to be mutable. Consider the other methods discussed if you need to pass a mutable reference to the string.
Concat() Method
If you have a vector of strings, you can concatenate by using the concat() method. This method will expand the vector into a single string value.
An example code is as shown:
let string: String = arr.concat();
println!("{}", string);
The previous example takes advantage of the array’s power to concatenate the strings. You do not have to use a dynamic array (vector). You can also use an array to perform the same operation.
Join Method
The join method is closely similar to the concat() method. However, it provides the ability to provide a separator when concatenating the strings.
For example:
let string: String = arr.join(" ");
println!("{}", string);
The previous code uses the join method to concatenate the strings inside the array using a space as a separator.
NOTE: The join method is a renamed version of the connect method.
Format! Macro
The other technique you can use to concatenate a string is the format! macro. It behaves similarly to the concat! macro as shown:
let str2 = " world".to_string();
let full_string = format!("{}{}", str1, str2);
println!("{}", full_string);
The previous code uses the format! macro to concatenate strings.
Conclusion
In this article, we discuss various methods and techniques to concatenate strings in the Rust language. These methods include the + Operator, Concat! Macro, Push_st() method, Concat() method, Join method, and Format! Marco We hope you found this article helpful. Check the other Linux Hint articles for more tips and information.