Lesson 8 - Strings

Rust has two commonly used text types:

String
&str

Understanding the difference will become especially important when you learn ownership.

let message = "Hello!";

This is a string slice, or &str.

String literals are fixed pieces of text written directly into your program.

let message = String::from("Hello!");

Unlike a string literal, a String is an owned and growable piece of text.

Use push_str to add a string slice to the end of the String.

let mut message = String::from("Hello");

message.push_str(" world!");

println!("{}", message);

To add a single character use push like this: message.push('!');

To find the length of a String, use len.

println!("{}", message.len()); would print the length of the string.