Module collections::string [] [src]

A UTF-8 encoded, growable string.

This module contains the String type, a trait for converting ToStrings, and several error types that may result from working with Strings.

Examples

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

fn main() { let s = "Hello".to_string(); let s = String::from("world"); let s: String = "also this".into(); }
let s = "Hello".to_string();

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

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

fn main() { let s = "Hello".to_string(); let message = s + " world!"; }
let s = "Hello".to_string();

let message = s + " world!";

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

fn main() { 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]); }
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]);

Reexports

use core::prelude::v1::*;
use core::fmt;
use core::hash;
use core::iter::FromIterator;
use core::mem;
use core::ops::{self, Add};
use core::ptr;
use core::slice;
use core::str::pattern::Pattern;
use rustc_unicode::char::{decode_utf16, REPLACEMENT_CHARACTER};
use rustc_unicode::str as unicode_str;
use borrow::{Cow, IntoCow};
use range::RangeArgument;
use str::{self, FromStr, Utf8Error, Chars};
use vec::Vec;
use boxed::Box;

Structs

Drain

A draining iterator for String.

FromUtf16Error

A possible error value when converting a String from a UTF-16 byte slice.

FromUtf8Error

A possible error value when converting a String from a UTF-8 byte vector.

String

A UTF-8 encoded, growable string.

Enums

ParseError

An error when parsing a String.

Traits

ToString

A trait for converting a value to a String.