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 typestrcannot be indexed by{integer}", because byte positions and characters don't line up in UTF-8. Later lessons show the proper ways, such aschars().
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:
- Write
println!and open the parentheses. - Type the text between double quotes:
"I love coding!". - 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
\".
