Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

String slice type

The string slice (str) type represents a sequence of characters.

#![allow(unused)]
fn main() {
let greeting1: &str = "Hello, world!";
let greeting2: &str = "你好,世界";
}

Note

See the standard library docs for information on the impls of the str type.

A value of type str is represented in the same way as [u8], a slice of 8-bit unsigned bytes.

Note

The standard library makes extra assumptions about str: methods working on str assume and ensure that the data it contains is valid UTF-8. Calling a str method with a non-UTF-8 buffer can cause undefined behavior now or in the future.

A str is a dynamically sized type. It can only be instantiated through a pointer type, such as &str. The layout of &str is the same as the layout of &[u8].