String Slices
In this page:
Creating a String Slice
A string slice references part of a String's data using a byte-range index. It does not own or copy the text -- it simply points into the original string's memory.
Example: Creating a String Slice
fn main() {
let greeting = String::from("Hello, world!");
let hello = &greeting[0..5];
println!("{}", hello);
}
Login to try C/C++/Java/PHP code in the editor
String Literals Are Slices
A string literal written directly in source code, like "Rust", has the type &str and lives for the entire duration of the program, since it is embedded directly in the compiled binary.
Example: String Literals Are Slices
fn main() {
let literal: &str = "I am already a slice";
println!("{}", literal);
}
Login to try C/C++/Java/PHP code in the editor
Accepting &str in Functions
Writing a function parameter as &str instead of String lets it accept both borrowed String data and string literals, making the function more flexible for callers.
Example: Accepting &str in Functions
fn first_word(text: &str) -> &str {
match text.find(' ') {
Some(index) => &text[..index],
None => text,
}
}
fn main() {
let sentence = String::from("Rust is fun");
println!("{}", first_word(&sentence));
}
Login to try C/C++/Java/PHP code in the editor
Slicing on Character Boundaries
Because Rust strings are UTF-8 encoded, slice indices must land on valid character boundaries. Slicing through the middle of a multi-byte character causes a runtime panic, so text-safe code often uses methods like .chars() instead of raw byte indices.
Example: Slicing on Character Boundaries
fn main() {
let word = "Rust";
let piece = &word[0..2];
println!("First two bytes: {}", piece);
}
Login to try C/C++/Java/PHP code in the editor
- Trying to slice a
Stringin the middle of a multi-byte UTF-8 character, which panics at runtime. - Using
Stringeverywhere a function only needs to read text, when accepting&strwould work for both owned strings and literals. - Assuming
&strandStringare the same type --&stris a borrowed view whileStringis an owned, growable buffer.
&stris an immutable reference to some UTF-8 text, either borrowed from aStringor a string literal.- String slices use byte-index ranges, e.g.
&text[0..5], and must fall on valid UTF-8 character boundaries. - String literals like
"hello"are already&strslices with a'staticlifetime. - Functions that only need to read string data should typically accept
&strrather thanStringfor maximum flexibility.
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: