<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Maxwell DeMers</title>
    <description>The latest articles on DEV Community by Maxwell DeMers (@maxuuell).</description>
    <link>https://dev.to/maxuuell</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F159110%2F7326d3e4-8295-48f5-84ce-0c33f0889055.jpg</url>
      <title>DEV Community: Maxwell DeMers</title>
      <link>https://dev.to/maxuuell</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/maxuuell"/>
    <language>en</language>
    <item>
      <title>How to Concatenate Strings in Rust</title>
      <dc:creator>Maxwell DeMers</dc:creator>
      <pubDate>Tue, 15 Sep 2020 04:44:36 +0000</pubDate>
      <link>https://dev.to/maxuuell/how-to-concatenate-strings-in-rust-k94</link>
      <guid>https://dev.to/maxuuell/how-to-concatenate-strings-in-rust-k94</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;When I was working on &lt;a href="https://github.com/maxuuell/swcli"&gt;swcli&lt;/a&gt;, 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Good ol' + Operator
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"http://swapi.dev/api/"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="py"&gt;.attributes&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"/"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="py"&gt;.id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I just tried this as a carry over from the pre-&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals"&gt;template literal&lt;/a&gt; days. This is one of the ways you could concatenate strings in Javascript.&lt;/p&gt;

&lt;p&gt;I should point out a limitation to using the &lt;code&gt;+&lt;/code&gt; operator that Chris Biscardi called out in &lt;a href="https://www.christopherbiscardi.com/concatenating-two-string-slices-and-str-in-rust"&gt;his post&lt;/a&gt;. You can only use the &lt;code&gt;+&lt;/code&gt; operator on &lt;em&gt;owned&lt;/em&gt; values which means you can't use &lt;code&gt;+&lt;/code&gt; with string slices.&lt;/p&gt;

&lt;h2&gt;
  
  
  Format Macro
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;format!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"http://swapi.dev/api/{}/{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="py"&gt;.attributes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="py"&gt;.id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Straight from the &lt;a href="https://doc.rust-lang.org/std/macro.format.html"&gt;rust docs&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A common use for format! is concatenation and interpolation of strings.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Use &lt;code&gt;push_str&lt;/code&gt; on a &lt;code&gt;String&lt;/code&gt; struct
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"http://swapi.dev/api/"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="nf"&gt;.push_str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="py"&gt;.attributes&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="nf"&gt;.push_str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="nf"&gt;.push_str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="py"&gt;.id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Array.concat()
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"http://swapi.dev/api/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="py"&gt;.attributes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="py"&gt;.id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="nf"&gt;.concat&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This pattern leverages an array to concatenate the items together. This was recommended by &lt;a href="https://users.rust-lang.org/t/any-difference-between-these-concatenation-strategies/47094/5?u=maxuuell"&gt;scottmcm&lt;/a&gt; as the cleanest and fully efficient way of concatenating strings.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Actual &lt;code&gt;url&lt;/code&gt; Crate
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;paths&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="py"&gt;.attributes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="py"&gt;.id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="nf"&gt;.concat&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Url&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"http://swapi.dev/api/"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="nf"&gt;.join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;paths&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;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 &lt;a href="https://docs.rs/url/2.1.1/url/"&gt;url crate&lt;/a&gt;.&lt;/p&gt;

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

&lt;p&gt;If you need to concatenate strings for a more general purpose, follow the &lt;code&gt;.concat()&lt;/code&gt; recommendations above.&lt;/p&gt;

&lt;h2&gt;
  
  
  Edit Stackoverflow Response
&lt;/h2&gt;

&lt;p&gt;There is a &lt;a href="https://stackoverflow.com/questions/30154541/how-do-i-concatenate-strings"&gt;stackoverflow question&lt;/a&gt; that has a lot of great responses as well. Give it a look for some additional details.&lt;/p&gt;

&lt;p&gt;Happy coding! 🤓&lt;/p&gt;

</description>
      <category>rust</category>
    </item>
  </channel>
</rss>
