On this page
  1. str
  2. Availability
  3. Syntax & Overview
  4. Notes & Pitfalls
  5. Example

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

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