# Module alloc::string

1.0.0 · source ·
Expand description

A UTF-8–encoded, growable string.

This module contains the `String` type, the `ToString` trait for converting to strings, and several error types that may result from working with `String`s.

## Examples

There are multiple ways to create a new `String` from a string literal:

    let s = "Hello".to_string();

    let s = String::from("world");
    let s: String = "also this".into();

Run

You can create a new `String` from an existing one by concatenating with `+`:

    let s = "Hello".to_string();

    let message = s + " world!";

Run

If you have a vector of valid UTF-8 bytes, you can make a `String` out of it. You can do the reverse too.

    let sparkle_heart = vec![240, 159, 146, 150];

    // We know these bytes are valid, so we'll use `unwrap()`.
    let sparkle_heart = String::from_utf8(sparkle_heart).unwrap();

    assert_eq!("💖", sparkle_heart);

    let bytes = sparkle_heart.into_bytes();

    assert_eq!(bytes, [240, 159, 146, 150]);

Run

## Structs

- Drain
  A draining iterator for `String`.
- FromUtf8Error
  A possible error value when converting a `String` from a UTF-8 byte vector.
- FromUtf16Error
  A possible error value when converting a `String` from a UTF-16 byte slice.
- String
  A UTF-8–encoded, growable string.

## Traits

- ToString
  A trait for converting a value to a `String`.

## Type Definitions

- ParseError
  A type alias for `Infallible`.