How to Concatenate Strings in Rust
Tags: rust
Published: 2020-09-15 04:44:36

---
title: How to Concatenate Strings in Rust
published: true
description: 5 different ways to concatenate strings in Rust
tags: Rust
cover_image: https://maxuuell.com/concatenate-strings.jpeg
canonical_url: https://maxuuell.com/blog/how-to-concatenate-strings-in-rust
---

If you are new to Rust, you many have to re-learn how to do certain tasks that you almost did automatically in other languages.

When I was working on [swcli](https://github.com/maxuuell/swcli), I needed to create a URL dynamically, based on user input. I ended up finding 5 approaches to create the URL. Now, I am going to share those 5 approaches, and highlight which ones may be the best going forward.

## Good ol' + Operator

```rust
let url = String::from("http://swapi.dev/api/") + &args.attributes + "/" + &args.id;
```

I just tried this as a carry over from the pre-[template literal](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) days. This is one of the ways you could concatenate strings in Javascript.

I should point out a limitation to using the `+` operator that Chris Biscardi called out in [his post](https://www.christopherbiscardi.com/concatenating-two-string-slices-and-str-in-rust). You can only use the `+` operator on _owned_ values which means you can't use `+` with string slices.

## Format Macro

```rust
let url = format!("http://swapi.dev/api/{}/{}", args.attributes, args.id);
```

Straight from the [rust docs](https://doc.rust-lang.org/std/macro.format.html):

> A common use for format! is concatenation and interpolation of strings.

## Use `push_str` on a `String` struct

```rust
let mut url = String::from("http://swapi.dev/api/");
url.push_str(&args.attributes);
url.push_str("/");
url.push_str(&args.id);
```

This pattern is great, but does require a mutable reference. Everything in Rust is immutable by default. If you don't want a mutable reference to your string, consider one of the other options.

## Array.concat()

```rust
let url = ["http://swapi.dev/api/", &args.attributes, "/", &args.id].concat();
```

This pattern leverages an array to concatenate the items together. This was recommended by [scottmcm](https://users.rust-lang.org/t/any-difference-between-these-concatenation-strategies/47094/5?u=maxuuell) as the cleanest and fully efficient way of concatenating strings.

## The Actual `url` Crate

```rust
let paths = [&args.attributes, "/", &args.id].concat();
let url = Url::parse("http://swapi.dev/api/")?;
let url = url.join(&paths)?;
```

If you recall, I needed to create a URL dynamically. What I should have started with, and what you should too if you need to create a URL, is just use the [url crate](https://docs.rs/url/2.1.1/url/).

The `.join()` method only allows you to append one string to the end of the url and path. In this case, I had to use the `.concat()` approach mentioned above to concatenate my cli arguments to then join them to the base url.

If you need to concatenate strings for a more general purpose, follow the `.concat()` recommendations above.

## Edit Stackoverflow Response

There is a [stackoverflow question](https://stackoverflow.com/questions/30154541/how-do-i-concatenate-strings) that has a lot of great responses as well. Give it a look for some additional details.

Happy coding! 🤓