Skip to content

std::strings

Status: experimental

String operations.

API details and source

The implementation source contains the complete declarations and implementation notes. The table below lists canonical Gossamer call signatures; every item name links directly to its implementation file.

Item Canonical signature or declaration Description
bytes fn bytes(text: String) -> Vec<u8> Returns the UTF-8 bytes of the string.
center fn center(text: String, width: i64, fill: char) -> String Symmetric pad to width using the given pad character.
chars fn chars(text: String) -> Vec<char> Returns the Unicode scalar values of the string.
contains fn contains(text: String, needle: String | char) -> bool Returns whether the string contains a substring.
contains_any fn contains_any(text: String, needle: String | char) -> bool Reports whether the string contains any rune in a set.
count fn count(text: String, needle: String | char) -> i64 Counts non-overlapping occurrences of needle.
ends_with fn ends_with(text: String, needle: String | char) -> bool Returns whether the string ends with the given suffix.
equal_fold fn equal_fold(text: String, needle: String | char) -> bool Case-insensitive Unicode string equality.
find fn find(text: String, needle: String | char) -> Option<i64> Returns the byte position of the first match.
find_any fn find_any(text: String, needle: String | char) -> Option<i64> Byte index of the first rune in a set, or None.
join fn join(parts: Vec<String>, sep: String) -> String Joins string parts with a separator.
lines fn lines(text: String) -> Vec<String> Splits into lines, dropping line terminators.
pad_left fn pad_left(text: String, width: i64, fill: char) -> String Left-pads to width with the given character.
pad_right fn pad_right(text: String, width: i64, fill: char) -> String Right-pads to width with the given character.
parse fn parse<T>(text: String) -> Result<T, errors::Error> Parses the string into the expected Result payload type.
repeat fn repeat(text: String, count: i64) -> String Concatenates n copies of the string.
replace fn replace(text: String, from: String | char, to: String | char) -> String Replaces every occurrence of from with to.
replacen fn replacen(text: String, from: String | char, to: String | char, n: i64) -> String Replaces the first n occurrences of a substring.
rfind fn rfind(text: String, needle: String | char) -> Option<i64> Byte index of the last occurrence of a needle, or -1.
rfind_any fn rfind_any(text: String, needle: String | char) -> Option<i64> Byte index of the last rune in a set, or None.
rsplit_once fn rsplit_once(text: String, sep: String | char) -> Option<(String, String)> Splits on the last occurrence of sep; returns Option<(String, String)>.
slice fn slice(text: String, start: i64, end: i64) -> Result<String, errors::Error> Safe byte-range slice returning Result.
split fn split(text: String, sep: String | char) -> Vec<String> Splits a string by a delimiter.
split_once fn split_once(text: String, sep: String | char) -> Option<(String, String)> Splits on the first occurrence of sep; returns Option<(String, String)>.
split_whitespace fn split_whitespace(text: String) -> Vec<String> Splits on runs of whitespace, dropping empty fields.
splitn fn splitn(text: String, n: i64, sep: String | char) -> Vec<String> Splits a string into at most n parts.
starts_with fn starts_with(text: String, needle: String | char) -> bool Returns whether the string starts with the given prefix.
strip_prefix fn strip_prefix(text: String, prefix: String | char) -> Option<String> Removes a leading prefix if present.
strip_suffix fn strip_suffix(text: String, suffix: String | char) -> Option<String> Removes a trailing suffix if present.
to_bool fn to_bool(text: String) -> Result<bool, errors::Error> Parses exactly true / false to Option.
to_f64 fn to_f64(text: String) -> Result<f64, errors::Error> Strict full-string parse to Option.
to_i64 fn to_i64(text: String) -> Result<i64, errors::Error> Strict full-string parse to Option.
to_lowercase fn to_lowercase(text: String) -> String Lowercases every character.
to_title fn to_title(text: String) -> String Title-cases the first letter of each word.
to_uppercase fn to_uppercase(text: String) -> String Uppercases every character.
trim fn trim(text: String) -> String Removes leading and trailing whitespace.
trim_end fn trim_end(text: String) -> String Removes trailing whitespace.
trim_end_matches fn trim_end_matches(text: String, cutset: String | char) -> String Removes trailing characters in the given set.
trim_matches fn trim_matches(text: String, cutset: String | char) -> String Removes characters in the given set from both ends.
trim_start fn trim_start(text: String) -> String Removes leading whitespace.
trim_start_matches fn trim_start_matches(text: String, cutset: String | char) -> String Removes leading characters in the given set.