Primitive Type str
[-] [+]
Unicode string manipulation (str
type)
Basic Usage
Rust's string type is one of the core primitive types of the language. While
represented by the name str
, the name str
is not actually a valid type in
Rust. Each string must also be decorated with a pointer. String
is used
for an owned string, so there is only one commonly-used str
type in Rust:
&str
.
&str
is the borrowed string type. This type of string can only be created
from other strings, unless it is a static string (see below). As the word
"borrowed" implies, this type of string is owned elsewhere, and this string
cannot be moved out of.
As an example, here's some code that uses a string.
fn main() { let borrowed_string = "This string is borrowed with the 'static lifetime"; }fn main() { let borrowed_string = "This string is borrowed with the 'static lifetime"; }
From the example above, you can guess that Rust's string literals have the
'static
lifetime. This is akin to C's concept of a static string.
More precisely, string literals are immutable views with a 'static lifetime
(otherwise known as the lifetime of the entire program), and thus have the
type &'static str
.
Representation
Rust's string type, str
, is a sequence of Unicode scalar values encoded as a
stream of UTF-8 bytes. All strings are
guaranteed to be validly encoded UTF-8 sequences. Additionally, strings are
not null-terminated and can thus contain null bytes.
The actual representation of strings have direct mappings to slices: &str
is the same as &[u8]
.
Trait Implementations
impl Repr<Slice<u8>> for str
impl Ord for str
impl PartialEq<str> for str
impl Eq for str
fn assert_receiver_is_total_eq(&self)
impl PartialOrd<str> for str
fn partial_cmp(&self, other: &str) -> Option<Ordering>
fn lt(&self, &str) -> bool
fn le(&self, &str) -> bool
fn gt(&self, &str) -> bool
fn ge(&self, &str) -> bool
impl Index<Range<usize>> for str
Returns a slice of the given string from the byte range
[begin
..end
).
This operation is O(1)
.
Panics when begin
and end
do not point to valid characters
or point beyond the last character of the string.
Example
fn main() { let s = "Löwe 老虎 Léopard"; assert_eq!(&s[0 .. 1], "L"); assert_eq!(&s[1 .. 9], "öwe 老"); // these will panic: // byte 2 lies within `ö`: // &s[2 ..3]; // byte 8 lies within `老` // &s[1 .. 8]; // byte 100 is outside the string // &s[3 .. 100]; }let s = "Löwe 老虎 Léopard"; assert_eq!(&s[0 .. 1], "L"); assert_eq!(&s[1 .. 9], "öwe 老"); // these will panic: // byte 2 lies within `ö`: // &s[2 ..3]; // byte 8 lies within `老` // &s[1 .. 8]; // byte 100 is outside the string // &s[3 .. 100];
impl Index<RangeTo<usize>> for str
Returns a slice of the string from the beginning to byte
end
.
Equivalent to self[0 .. end]
.
Panics when end
does not point to a valid character, or is
out of bounds.
impl Index<RangeFrom<usize>> for str
Returns a slice of the string from begin
to its end.
Equivalent to self[begin .. self.len()]
.
Panics when begin
does not point to a valid character, or is
out of bounds.