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, 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
let url = String::from("swapi.dev/api") + &args.attributes + "/" + &args.id;
I just tried this as a carry over from the pre-template literal 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. You can only use the +
operator on owned values which means you can't use +
with string slices.
Format Macro
let url = format!("swapi.dev/api/{}/{}", args.attributes, args.id);
Straight from the rust docs:
A common use for format! is concatenation and interpolation of strings.
Use push_str
on a String
struct
let mut url = String::from("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()
let url = ["swapi.dev/api", &args.attributes, "/", &args.id].concat();
This pattern leverages an array to concatenate the items together. This was recommended by scottmcm as the cleanest and fully efficient way of concatenating strings.
The Actual url
Crate
let paths = [&args.attributes, "/", &args.id].concat();
let url = Url::parse("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.
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 that has a lot of great responses as well. Give it a look for some additional details.
Happy coding! 🤓