avatarAnkit Tanna

Summary

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2872

Abstract

n class="hljs-keyword">fn</span> <span class="hljs-title function_">main</span>() { <span class="hljs-keyword">let</span> <span class="hljs-variable">greeting</span> = <span class="hljs-string">"Hello"</span>; <span class="hljs-keyword">let</span> <span class="hljs-variable">subject</span> = <span class="hljs-string">"World"</span>;

<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}, {}"</span>, greeting, subject);

}</pre></div><p id="e394">As you can see, we have 2 variables <code>greeting</code> and <code>subject</code> which are logged using <code>println</code> function. The <code>println</code> method has 2 interpolation placeholders( <code>{}</code> ) which are used to replace <code>greeting</code> and <code>subject</code> respectively.</p><p id="430e">Compiling this file using <code>rustc</code> also gives us a binary and executing the binary gives us the below output:</p><figure id="f885"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Xez_pWgHj_LPztOfXI5yEQ.png"><figcaption></figcaption></figure><p id="a97b">Rust does not have a built in string interpolation like Python or JavaScript. The wierd way that we saw above substitutes the curly braces with <code>greetings</code> and <code>subject</code>.</p><h2 id="c30b">“format” method</h2><p id="b2f1">There is another method which does the string interpolation. The only difference is, it does not print the string to the console. It just returns the string.</p><p id="a4c3">Lets have a look at the below code:</p><div id="3a84"><pre><span class="hljs-keyword">fn</span> <span class="hljs-title function_">main</span>() { <span class="hljs-keyword">let</span> <span class="hljs-variable">subject</span> = <span class="hljs-string">"world"</span>; <span class="hljs-keyword">let</span> <span class="hljs-variable">greeting</span> = <span class="hljs-built_in">format!</span>(<span class="hljs-string">"Hello, {}!"</span>, subject); <span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, greeting); }</pre></div><h2 id="581c">Observations from the code:</h2><ol><li><code>format</code> does not print the string in console, <code>println</code> does the printing</li><li><code>format</code> follows the same <code>{}</code> substitution pattern</li><li>It can be used to do the string manipulation in your code</li></ol><h2 id="cdcd">Output:</h2><figure id="16dd"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*pSGK36ppcSwFofo_D5870w.png"><figcaption></figcaption></figure><p id="84db"><code>println</code> has quite a weird syntax but the error messages are quite helpful so you can easily understand them. One of the examples of error message is as below:</p><figure id="3ed7"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*VgVz9HuOD6rsrUT3G8451Q.png"><figcaption></

Options

figcaption></figure><h2 id="ecae">Observations from the error messages from Rust Compiler:</h2><ol><li>The <code>error</code> message at the top is technical</li><li>It does mention the line number and column number along with the file name in which the error occurred</li><li>It also gives you the hint under <code>help</code> trying to understand possible solutions.</li></ol><h2 id="fac9">“panic” method</h2><p id="6ec7">Another method that uses string interpolation in Rust is <code>panic</code>. <code>panic</code> is a method which terminates the program when it is encountered. It is not like the <code>throwError</code> like other programming languages.</p><div id="6e5d"><pre><span class="hljs-keyword">fn</span> <span class="hljs-title function_">main</span>() { <span class="hljs-keyword">let</span> <span class="hljs-variable">crash_reason</span> = <span class="hljs-string">"server took a nap"</span>; <span class="hljs-built_in">panic!</span>(<span class="hljs-string">"I crashed {}!"</span>, crash_reason); <span class="hljs-built_in">println!</span>(<span class="hljs-string">"This will never get printed out!"</span>); }</pre></div><p id="1dd2">The code does compile but with a warning:</p><figure id="c607"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Ha3s2Yvdr1heXLmSdrbBgw.png"><figcaption></figcaption></figure><p id="8014">Any line after the <code>panic</code> call is not reachable.</p><h2 id="14ab">Output:</h2><figure id="16e3"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*OafU4ijV17rF5mAuuNj1cA.png"><figcaption></figcaption></figure><p id="1c1d">If you enable the the RUST_BACKTRACE, which we’ll talk about later, it will give you the trace of the error from where it has originated.</p><figure id="2bde"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*kmEifiP0YMHqFmuxDTzmUw.png"><figcaption></figcaption></figure><p id="eca9">Please share your feedback about this article.</p><p id="6161">You can subscribe to my newsletter about <b>The Rust Programming Language <a href="https://tinyletter.com/ankittanna"></a></b><a href="https://tinyletter.com/ankittanna">here</a>. You can read about all the articles in this series <a href="https://readmedium.com/the-rust-programming-language-4b22bc717ecc">here</a>.</p><h2 id="7a6c">Rustaceans 🚀</h2><p id="5d03">Thank you for being a part of the Rustaceans community! Before you go:</p><ul><li>Show your appreciation with a clap and follow the publication</li><li>Discover how you can contribute your own insights to <a href="https://readmedium.com/rustaceans-submissions-are-now-open-924e67cc595d"><b><i>Rustaceans</i></b></a>.</li><li>Connect with us: <a href="https://twitter.com/rustaceans_rs"><b><i>X</i></b></a> | <a href="https://substack.com/@weeklyrust"><b><i>Weekly Rust Newsletter</i></b></a></li></ul></article></body>

The Rust Programming Language

The Rust Programming Language — Primitives — String

We will learn about Primitive types in Rust programming language. One of the very first type is string.

Rust files have an extension of .rs

Step 1: Install Rust

You need to install Rust on your computer before you can start compiling Rust code.

After installing the rust, you will have access to rustc which is a way to invoke Rust compiler to read through your files and compile them to executables.

Step 2: Create a rust file

We’ll now create a rust file: app.rs

Below is the code snippet for reference:

fn main() {
    println!("Hello, World!");
}

Step 3: Observations from code

  1. fn is the keyword for function
  2. main is the first function that gets triggered, just like programming languages like Java
  3. A function call has an ! at the end of the name of the function. We’ll address that in upcoming posts
  4. println is the function which is equivalent to console.log in JavaScript or System.out.println in Java

Step 4: Compiling the code

In order to run this code you need to first generate a binary/executable of this code, just like how C/C++ or Java does. You can do this by running rustc command

  1. Go to the directory where your app.rs file is present
  2. Execute rustc app.rs
  3. Notice that a binary/executable file would have been generated
  4. You should now have 2 files in your

Step 5: Executing the code

  1. Double-click the binary file

String interpolation

There are cases where we need String Interpolation for joining two strings. Rust has a very wierd syntax to do so. It uses {} as the placeholder for the variable you wish to interpolate.

Refer below snippet for example:

fn main() {
    let greeting = "Hello";
    let subject = "World";

    println!("{}, {}", greeting, subject);
}

As you can see, we have 2 variables greeting and subject which are logged using println function. The println method has 2 interpolation placeholders( {} ) which are used to replace greeting and subject respectively.

Compiling this file using rustc also gives us a binary and executing the binary gives us the below output:

Rust does not have a built in string interpolation like Python or JavaScript. The wierd way that we saw above substitutes the curly braces with greetings and subject.

“format” method

There is another method which does the string interpolation. The only difference is, it does not print the string to the console. It just returns the string.

Lets have a look at the below code:

fn main() {
    let subject = "world";
    let greeting = format!("Hello, {}!", subject);
    println!("{}", greeting);
}

Observations from the code:

  1. format does not print the string in console, println does the printing
  2. format follows the same {} substitution pattern
  3. It can be used to do the string manipulation in your code

Output:

println has quite a weird syntax but the error messages are quite helpful so you can easily understand them. One of the examples of error message is as below:

Observations from the error messages from Rust Compiler:

  1. The error message at the top is technical
  2. It does mention the line number and column number along with the file name in which the error occurred
  3. It also gives you the hint under help trying to understand possible solutions.

“panic” method

Another method that uses string interpolation in Rust is panic. panic is a method which terminates the program when it is encountered. It is not like the throwError like other programming languages.

fn main() {
    let crash_reason = "server took a nap";
    panic!("I crashed {}!", crash_reason);
    println!("This will never get printed out!");
}

The code does compile but with a warning:

Any line after the panic call is not reachable.

Output:

If you enable the the RUST_BACKTRACE, which we’ll talk about later, it will give you the trace of the error from where it has originated.

Please share your feedback about this article.

You can subscribe to my newsletter about The Rust Programming Language here. You can read about all the articles in this series here.

Rustaceans 🚀

Thank you for being a part of the Rustaceans community! Before you go:

  • Show your appreciation with a clap and follow the publication
  • Discover how you can contribute your own insights to Rustaceans.
  • Connect with us: X | Weekly Rust Newsletter
Rust
Rustlang
Rust Programming Language
Programming
Webassembly
Recommended from ReadMedium