Rust Tutorials: String vs &str

beginner
25 min

Rust Tutorials: String vs &str

Welcome to this comprehensive guide on Rust's String and &str! 🎯 In this tutorial, we'll dive deep into understanding these two essential string-related types in Rust and learn how to use them effectively. Let's get started!

Introduction 📝

Before we dive into the details, let's first understand what String and &str are.

  • String: A growable, heap-allocated vector of UTF-8 encoded bytes that represents a text string.
  • &str: A borrowed, immutable reference to a slice of UTF-8 encoded bytes that represents a text string.

String 💡

Creating a String

To create a String, we can use the String struct and the new() method:

rust
let my_string = String::new();

You can also create a String by directly providing the content:

rust
let my_string = String::from("Hello, World!");

Basic Operations

Some basic operations you can perform on a String include:

  • Getting the length of a String using the len() method:
rust
let my_string = String::from("Hello, World!"); let length = my_string.len(); // length is 13
  • Checking if a String is empty using the is_empty() method:
rust
let my_string = String::new(); let is_empty = my_string.is_empty(); // is_empty is true
  • Accessing a character in a String using the chars() method and the nth() method:
rust
let my_string = String::from("Hello, World!"); let third_char = my_string.chars().nth(2).unwrap(); // third_char is 'l'

&str 💡

Creating an &str

An &str is automatically created when you reference a string literal in Rust:

rust
let my_str: &str = "Hello, World!";

Basic Operations

Some basic operations you can perform on an &str include:

  • Getting the length of an &str using the len() method:
rust
let my_str: &str = "Hello, World!"; let length = my_str.len(); // length is 13
  • Accessing a character in an &str using the chars() method and the nth() method:
rust
let my_str: &str = "Hello, World!"; let third_char = my_str.chars().nth(2).unwrap(); // third_char is 'l'

String vs &str 💡

Now, you might be wondering, "What's the difference between String and &str?" Let's explore that!

Key Differences 💡

  • Allocation: String is allocated on the heap, while &str is a reference to a string slice on the stack.
  • Ownership: String has ownership over its memory, while &str does not.
  • Mutability: String is mutable, while &str is immutable.

When to Use Which 💡

  • Use String when you need to store and modify a string.
  • Use &str when you want to reference a string without owning it or when you need to pass a string to a function without moving it.

Advanced Examples 💡

Let's explore some advanced examples involving String and &str.

Converting Between String and &str 💡

You can convert a String to an &str using the as_str() method:

rust
let my_string = String::from("Hello, World!"); let my_str: &str = my_string.as_str();

You can also convert an &str to a String using the to_string() method:

rust
let my_str: &str = "Hello, World!"; let my_string = my_str.to_string();

Performance Considerations 💡

Since String is allocated on the heap, it has a larger memory footprint and slower performance compared to &str. However, when you need to store and modify a string, the additional memory usage and performance impact is often worth it.

Quiz Time 🎯

Quick Quiz
Question 1 of 1

What is the difference between a `String` and an `&str` in Rust?


That's it for our tutorial on Rust's String and &str! By now, you should have a solid understanding of these two essential string-related types in Rust. Happy coding! 🚀