Start learning free

LESSON 2 OF 348 · WELCOME TO RUST

Introduction to Strings in Rust in Rust

Lesson

TL ; DR:

  • To create a string in Rust, we put some text inside double quotes ("Hello world").


  • Be careful not to forget any quote! This code missing a " at the end:

    println!("Welcome!;

    would throw a SyntaxError



Full lesson:

How boring would our life as humans be if we communicated only using numbers? Luckily we have letters, words and languages to express what we think!

We can also use letters and words in Rust to express more meaningful messages like we do in real life.


Strings:

In programming, a string is any block of text e.g. any sequence of characters from your keyboard (letters, numbers, spaces, symbols, etc.).

To create a string in Rust, we put some text inside double quotes like this "Hello world".

For example, we can use strings with println!() for printing messages to the console:

println!("My name is Andy");
println!("Welcome to AlgoCademy!");

The output of this code is:

My name is Andy
Welcome to AlgoCademy!

As you can see, the quotes are not printed. That's because quotes are not part of the string. Their job is solely to let Rust know when a string declaration starts and when it ends.


Assignment
Follow the Coding Tutorial and let's work with strings!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    Introduction To Strings in Rust

    Keep going with RustAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free