๐Ÿ†’ Rust Text

General

  • Strings are stored using UTF-8
    • string = Vec<u8> with well formed UTF-8
    • โ†’ strings are not arrays of chars
  • Strings are immutable
    • โ†’ use String for dynamic ones
      • "text".to_string()
      • format!("text")

Types

char

  • <char> = 32-bit value holding a Unicode code point
  • Functions:
    • .is_numeric(), is_alphabetic(), .is_whitespace โ€ฆ

str

= Borrowed strings, fixed size

  • <str>
  • Create: "text"
  • Multi-line: r###"text"###

Strings

= Owned strings, dynamic size

  • <String> (= wrapper around Vec<u8>, which ensures valid UTF-8)
  • Create:
    • String::new()
    • "text".to_string()
    • String::with_capacity(n)

Byte Strings

  • Create: b"byte_text"

Gotchas

Not every source of a string is guaranteed to be valid UTF-8, solutions:

  • Filenames: std::path::PathBuf and &Path
  • Unencoded data: Vec<u8> and &[u8]
  • Env vars / cmd arguments: OsString and &OsStr