Rust Guide > Documentation > Collections > str

Introduction

In Rust, str is a primitive string slice type that represents an immutable sequence of UTF-8 bytes. It is usually seen in its borrowed form, &str, which references a slice of a String or a string literal. The str type provides methods for string manipulation, searching, and iteration.

Using str

To use str, you typically work with string literals or slices of a String. String slices can be created by borrowing a String or directly using string literals.

fn main() {
    let s: &str = "Hello, world!";
    println!("String slice: {}", s);

    let string = String::from("Hello, Rust!");
    let slice: &str = &string[0..5];
    println!("String slice from String: {}", slice);
}
// Output:
// String slice: Hello, world!
// String slice from String: Hello

Key Methods

Below are some of the key methods exposed by the str class:

len

Returns the length of the string slice in bytes.

fn main() {
    let s = "Hello, world!";
    println!("Length: {}", s.len());
}
// Output:
// Length: 13

is_empty

Returns true if the string slice is empty.

fn main() {
    let s = "";
    println!("Is empty: {}", s.is_empty());
}
// Output:
// Is empty: true

contains

Checks if the string slice contains a specified substring.

fn main() {
    let s = "Hello, world!";
    println!("Contains 'world': {}", s.contains("world"));
}
// Output:
// Contains 'world': true

starts_with

Checks if the string slice starts with a specified prefix.

fn main() {
    let s = "Hello, world!";
    println!("Starts with 'Hello': {}", s.starts_with("Hello"));
}
// Output:
// Starts with 'Hello': true

ends_with

Checks if the string slice ends with a specified suffix.

fn main() {
    let s = "Hello, world!";
    println!("Ends with 'world!': {}", s.ends_with("world!"));
}
// Output:
// Ends with 'world!': true

split

Splits the string slice by a specified delimiter and returns an iterator over the substrings.

fn main() {
    let s = "Hello, world!";
    for part in s.split(", ") {
        println!("{}", part);
    }
}
// Output:
// Hello
// world!

to_string

Converts the string slice to an owned String.

fn main() {
    let s = "Hello, world!";
    let string = s.to_string();
    println!("Converted to String: {}", string);
}
// Output:
// Converted to String: Hello, world!

Example Usage

Example 1: Basic String Slice

fn main() {
    let s: &str = "Rust is awesome!";
    println!("String slice: {}", s);
}
// Output:
// String slice: Rust is awesome!

Example 2: Length of a String Slice

fn main() {
    let s = "Rust";
    println!("Length: {}", s.len());
}
// Output:
// Length: 4

Example 3: Checking for Substring

fn main() {
    let s = "Rust programming";
    println!("Contains 'programming': {}", s.contains("programming"));
}
// Output:
// Contains 'programming': true

Example 4: Splitting a String Slice

fn main() {
    let s = "Rust,Go,Python";
    for language in s.split(',') {
        println!("{}", language);
    }
}
// Output:
// Rust
// Go
// Python

Example 5: Converting to String

fn main() {
    let s = "Convert me";
    let string = s.to_string();
    println!("Converted to String: {}", string);
}
// Output:
// Converted to String: Convert me

Considerations

  • str is a view into a string, and thus has no ownership of the data it references.
  • String slices are immutable; if you need a mutable string, consider using String.
  • UTF-8 encoding means that string operations based on byte indices must account for multi-byte characters.
  • When working with string slices, be careful with slicing operations to avoid panics due to out-of-bound indices.

See Also

  • String - An owned, growable string type for heap-allocated string data.
  • Vec - A growable array type, useful for dynamically-sized collections of elements.