Rust Lang

Rust Concat Strings

String concatenation is a very fundamental operation in any programming language. Therefore, understanding various methods to perform string concatenation can be very beneficial.

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:

fn main() {

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:

$ cargo run

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:

fn main() {

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:

$ cargo run

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:

fn main() {

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 arr = vec!["hello", " world"];

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 arr = ["hello", "world"];

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 str1 = "hello".to_string();

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.

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