Start learning free

LESSON 2 OF 414 · WELCOME TO RUST

Introduction to Strings in Rust: String Literals and println!

Lesson Read the full explanation

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 make the compiler report error[E0765]: unterminated double quote string



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

    RUST · WELCOME TO RUST

    Introduction To Strings in Rust, explained

    What a string is

    A string is a piece of text: any sequence of characters you can type, including letters, digits, spaces and symbols. Programs use strings for messages, names, file contents and anything else that is text rather than a number.

    In Rust, you write a string by putting the text between double quotes. This is called a string literal:

    "Hello world"
    

    The quotes are not part of the string. Their only job is to tell the compiler where the text begins and where it ends. A string literal has the type &str ("string slice"), a borrowed view of text that is stored inside the program itself.

    Printing strings

    You can pass a string literal straight to println!. When the text contains no placeholders, the text itself is the whole format string:

    println!("My name is Andy");
    println!("I love coding!");
    
    My name is Andy
    I love coding!
    

    Each println! ends its line, so the two messages appear on separate lines, and no quotes are printed.

    A string can also be stored in a variable and printed through a {} placeholder:

    let greeting = "Hello world";
    println!("{}", greeting);
    
    Hello world
    

    Strings versus numbers

    The string "12" and the number 12 look alike when printed, but they are different kinds of data. You can add 1 to a number, and you can't add 1 to a string. Quotes are what separate the two: 12 is a number, and "12" is text made of the characters 1 and 2. Rust won't convert one into the other behind your back.

    Quotes must come in pairs

    Every string needs both of its double quotes. If the closing one is missing, the compiler can't tell where the text stops:

    println!("Welcome!;
    

    This program does not compile. The error is "unterminated double quote string", and it points at the text that was never closed. The program never runs. Rust has no run-time SyntaxError: mistakes like this one are found by the compiler, before anything executes.

    Also, double quotes are for strings and single quotes are for single characters. 'a' is a char, one character, and 'hello' is an error, because a character literal may only contain one codepoint. Mixing the two kinds, as in 'Hey there!", doesn't work either.

    Quotes and backslashes inside a string

    To put a double quote inside a string, escape it with a backslash:

    println!("I said \"hi\"");
    
    I said "hi"
    

    Other escapes work the same way: \n is a new line, \t a tab, and \\ a single backslash.

    A first look at how Rust treats text

    Rust strings are UTF-8, so they can hold any language and emoji. Two consequences are worth knowing early:

    • "hello".len() counts bytes, not characters. For ASCII text the two are equal, but "é".len() is 2, while "é".chars().count() is 1.
    • You can't get a character with s[0]. The compiler rejects it with "the type str cannot be indexed by {integer}", because byte positions and characters don't line up in UTF-8. Later lessons show the proper ways, such as chars().

    There is also a second string type, String, which owns its text and can grow. You create one with String::from("hi") or "hi".to_string(). Literals such as "hi" are &str. For this lesson, literals are all you need.

    The lesson's exercise

    The exercise asks you to print the string I love coding!. Rust prints through the println! macro, so:

    1. Write println! and open the parentheses.
    2. Type the text between double quotes: "I love coding!".
    3. Close the parenthesis and add the semicolon.
    println!("I love coding!");
    

    The output is:

    I love coding!
    

    Type the text exactly as written, including the capital letter and the exclamation mark.

    Key takeaways

    • A string is text in double quotes, like "Hello world", and its type is &str.
    • The quotes mark the boundaries and are not printed.
    • A missing quote is a compile-time error, so the program doesn't run at all.
    • Double quotes make strings and single quotes make single characters.
    • Escape a double quote inside a string with \".
    Keep going with RustAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free