String concatenate in Rust

Get FREE domain for 1st year and build your brand new site

Free Linux Book

In this article, we have explored different ways we can concatenate multiple strings in Rust. This involves methods in Rust like connect() and macros in Rust like concat!().

You will learn how to concat / append / join multiple strings using various methods.

Table of contents:

  • concat!() macro
  • Using concat() method
  • Using connect() method
  • push_str()
  • using + operator
  • Using format!() macro
  • Owned and Borrowed Strings

Let us get started with String concatenate in Rust Programming Language.

concat!() macro

concat!() is a macro in Rust that is used to concatenate two strings into a static string. It takes multiple comma separated literals and the expression is of type &'static str. Following is a small code snippet:

let s = concat!("opengenus", "p", 4);
assert_eq!(s, "opengenusp4");

Example of using concat!() to concatenate two strings in Rust:

fn main() {
    println!("{}", concat!("open", "genus"))
}

Output will be:

opengenus

Using concat() method

concat() method in Rust flattens a slice of type T into a single value of type U.

let v = vec!["open", "genus"];

let s: String = v.concat();

println!("{}", s);

Output:

opengenus

Using connect() method

connect() method in Rust flattens a slice of type T into a single value of type U and is similar to concat() method. The additional feature in connect() method is that we can specify a string (known as separator) that will be placed between each string of the slice.

let v = vec!["open", "genus"];

let s: String = v.connect(" , ");

println!("{}", s);

Output:

open , genus

push_str()

In this function push_str(), the original string is modified so the variable storing the original string should be of mutable type which is done using mul keyword.

Example of using push_str() to concatenate two strings in Rust:

fn main() {
    let mut _a = "open".to_string();
    let _b = "genus".to_string();

    _a.push_str(&_b);
    println!("{}", _a);
}

Note: The second string variable _b is not mutable as the function push_str() does not modify it.

Output:

opengenus

using + operator

Example of using + operator to concatenate two strings in Rust:

fn main() {
    let _a = "open".to_string();
    let _b = "genus".to_string();
    println!("{}", _a + &_b);
}

Note: In this case, none of the variable are mutable as the original variables are not modified by + operator for concatenate provided the same variable as not in the LHS.

Output:

opengenus

Using format!() macro

format!() is a macro like concat!() macro in Rust. Example of using format!() to concatenate two strings in Rust:

fn main() {
    let mut _a = "open".to_string();
    let _b = "genus".to_string();
    let _c = format!("{}{}", _a, _b);

    println!("{}", _c);
}

Output:

opengenus

Owned and Borrowed Strings

The output of string concatenation needs memory and hence, can be stored only in an owned string (not in a borrowed string in Rust). If the method you are using is modifying the first string to the concatenated string, then the first string should be an owned string only.

If none of the strings being concatenated are modified, then the strings can be owned string or borrowed string or a mixture of the both.

To create a owned string in Rust, you need to_owned as shown in this code example:

let owned_string: String = "open ".to_owned();

A borrowed string is like this:

let borrowed_string: &str = "world";

Following is the complete Rust code example where the first string is modified and hence, is the concatenated string:

fn main() {
    let mut owned_string: String = "open ".to_owned();
    let borrowed_string: &str = "genus";
    
    owned_string.push_str(borrowed_string);
    println!("{}", owned_string);
}

Output:

opengenus

Following is the complete Rust code example where both strings are borrowed strings:

fn main() {
    let borrowed_string1: &str = "open";
    let borrowed_string2: &str = "genus";
    
    let together = format!("{}{}", borrowed_string1, borrowed_string2);
    
    println!("{}", together);
}

Output:

opengenus

With this article at OpenGenus, you must have the complete idea of how to concatenate strings in Rust.