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
strtype.
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 onstrassume and ensure that the data it contains is valid UTF-8. Calling astrmethod 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].