Strings

Contents

Strings#

Two string types.

  • String: owned, heap-allocated, growable, UTF-8.

  • &str: borrowed string slice. Backed by a String, a string literal ('static), or a buffer.

Owned, growable.

let owned: String = String::from("hello");

Borrowed slice.

let borrowed: &str = "hello";

Borrow a String as a &str (auto-deref).

let r: &str = &owned;

Mutate the owned form in place.

owned.push_str(", world");

Get a new String from a method call.

let upper: String = owned.to_uppercase();

For bytes use Vec<u8> (owned) and &[u8] (borrowed slice); for byte string literals, b"hello".

References#