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

String

Prev: Vec | Up: builtins | Next: str
Rel: builtins, Ownership, Borrowing & Lifetimes
Ref: Rust Docs โ€” String
Tags: #public #prog #rust #builtins #tier/core


Availability

Built-in / Prelude (Prelude Type) โ€” available with zero use imports.

Syntax & Overview

String  /* owned UTF-8 buffer */

Heap-allocated, growable, guaranteed-valid UTF-8 string (essentially a Vec<u8> with UTF-8 invariant).

Notes & Pitfalls

Example

fn greet(name: &str) -> String {
    let mut out = String::from("Hello, ");
    out.push_str(name);
    out.push('!');
    out
}

fn main() {
    assert_eq!(greet("Ada"), "Hello, Ada!");
}

Linked from

Related notes