> built-in type
str
methods of str values
The methods below are on string values. They are available on any str value directly, without importing a module. The std::str module adds the chr function and the str::Error type.
Strings are UTF-8. Lengths, indices and offsets count characters (Unicode scalar values), not bytes: "αβ".len() is 2. Strings never change; methods that edit return a new string.
slice, insert and remove take indices that may be negative, counting from the end: -1 is the position before the last character. Indices past either end are clamped to the string, so these methods never fail.
> methods
The character index of the first occurrence of needle, or None.
Parses the string as pairs of hex digits, ignoring whitespace, the same as bytes::from_hex. The error is a bytes::Error: BadHex for an odd number of digits or a non-hex character.
The string with substr inserted before the character at index: "helo".insert(3, "l") is "hello".
str.is_empty() -> boolWhether the string has no characters.
The lines of the string, split at \\n or \\r\\n. A final line ending does not add an empty line.
The Unicode codepoint of a one-character string: "A".ord() is Ok(65). Any other length gives Err(str::Error::NotSingleChar).
The string padded on the left or right to width characters, repeating the characters of fill as needed. A string already width long or longer, or an empty fill, leaves the string unchanged.
The string padded on the left or right to width characters, repeating the characters of fill as needed. A string already width long or longer, or an empty fill, leaves the string unchanged.
The string, without surrounding whitespace, as a float. Anything else gives Err(str::Error::Parse).
The string, without surrounding whitespace and an optional 0x or 0X prefix, as a hexadecimal integer: "0xff".parse_hex() is Ok(255). Anything else gives Err(str::Error::Parse).
The string, without surrounding whitespace, as a decimal integer. Anything else gives Err(str::Error::Parse).
The string without the characters from start up to, not including, end: "hello".remove(1, 4) is "ho". When start is not before end, the string is unchanged.
The character index of the last occurrence of needle, or None.
The characters from start up to, not including, end. "hello".slice(1, 4) is "ell" and "hello".slice(0, -1) is "hell". When start is not before end, the result is empty.
The pieces between occurrences of separator. Empty pieces are kept: "a,,b".split(",") is ["a", "", "b"].
> examples
use std::io;
fn main() {
let s = " Hello, World! ";
io::println("{}", s.trim());
io::println("{}", s.trim().to_upper());
io::println("{}", "a,b,c".split(",").join(" | "));
io::println("{}", "42".parse_int().unwrap());
}