On this page
str
Prev: String | Up: builtins | Next: Copy
Rel: builtins, Ownership, Borrowing & Lifetimes
Ref: Rust Docs — str
Tags: #public #prog #rust #builtins #tier/core
Availability
Built-in / Prelude (Primitive Slice) — available with zero use imports.
Syntax & Overview
&str /* borrowed UTF-8 string slice */
Fat pointer (ptr, byte_len) borrowing a valid UTF-8 sequence (string literals have type &'static str).
Notes & Pitfalls
- Unsized UTF-8 slice (
&str): Zero-allocation view into aStringor static binary literal (&'static str). - Slicing on a non-UTF-8 char boundary panics: Use
.get(a..b)for safe fallible slicing or.chars()for Unicode iteration.
Example
fn first_word(s: &str) -> &str {
s.split_whitespace().next().unwrap_or("")
}
fn main() {
assert_eq!(first_word(" rust stdlib "), "rust");
}
Linked from
Related notes
- tuts/rust/builtins/Copy Linked here; links to this page; shared tags: #builtins, #prog, #rust, #tier
- tuts/rust/builtins/String Linked here; links to this page; shared tags: #builtins, #prog, #rust, #tier
- tuts/rust/builtins Linked here; links to this page; shared tags: #builtins, #prog, #rust
- tuts/rust/ownership_and_internals/ownership_borrowing_and_lifetimes Linked here; shared tags: #prog, #rust
- tuts/rust/tutorials/zero_copy_parsing_with_lifetimes links to this page; shared tags: #prog, #rust
- tuts/rust/builtins/Clone shared tags: #builtins, #prog, #rust, #tier
- tuts/rust/builtins/Drop shared tags: #builtins, #prog, #rust, #tier
- tuts/rust/builtins/From shared tags: #builtins, #prog, #rust, #tier
- tuts/rust/builtins/Iterator shared tags: #builtins, #prog, #rust, #tier
- tuts/rust/builtins/macro_rules shared tags: #builtins, #prog, #rust, #tier