about
01/25/2024
RustBites - Strings
Rust Bite - Strings
literals, Strings, formats, iteration
- Rust strings cannot be indexed as characters
- Accessing a character at a known location is linear time
- Converting between Rust strings and those of the platform are more complex than for some other languages.
1. str - literal string
Table 1. - Selected str member functions:
member function | description |
---|---|
Converts a string slice to a byte slice | |
An iterator over bytes of a string slice | |
Returns iterator over chars of a string slice | |
Returns iterator over chars of a string slice, and their positions | |
Returns true if pattern P matches sub-slice of this string slice | |
Returns byte index of first character of this string slice that matches pattern P. Returns None if the pattern doesn't match. | |
Checks if all characters in this string are within ASCII range | |
Checks that index-th byte is first byte in UTF-8 code point sequence or end of the string | |
Returns true if self has length of zero bytes | |
This length is in bytes, not chars or graphemes | |
An iterator over the lines of a string, as string slices | |
Converts this string to its ASCII lower case equivalent in-place | |
Converts this string to its ASCII upper case equivalent in-place | |
Parses this string slice into another type | |
Creates a new String by repeating a string n times | |
Replaces all matches of pattern P with another string | |
An iterator over substrings of this string slice, separated by characters matched by a pattern | |
Returns a string slice with leading and trailing whitespace removed | |
More methods ... | std::string::String |
Basic str operations
2. String
Table 2. - String member functions:
member function | description |
---|---|
Create new empty String | |
Creates string from string slice | |
Returns string slice | |
Appends chars from s | |
Appends ch | |
Removes char at index n | |
inserts ch at location n | |
Inserts contents of s at location n | |
Returns length of string in bytes, not chars! They are the same only for ASCII characters. |
|
Returns true if len() == 0, else false | |
Removes all bytes | |
Converts vector of bytes to String. Returns error if invalid UTF-8 | |
Convert to Vec of bytes | |
Returns byte slice | |
Is this byte the start of a new UTF-8 character? | |
More methods ... | std::string::String |
String Examples:
3. String Formats
4. Iterating over Strings
Table 1. utf-8 character boundaries
char size | indicator |
---|---|
1 byte, e.g. ASCII | byte starts with bit 0 |
2 bytes | First byte starts with bits 110 |
3 bytes | First byte starts with bits 1110 |
4 bytes | First byte starts with bits 11110 |
not first byte | byte starts with bits 10 |
-
chars(&self) -> Chars<'_> Chars<'_> implementsnext(&self) -> Option<char> -
char_indices(&self) -> CharIndices<'_> CharIndices<'_> implementsnext(&self) -> Option<(usize, char)> -
bytes(&self) -> Bytes<'_> Bytes<'_> implementsnext(&self) -> Option<u8>
5. Other String Types
String Type | Description |
---|---|
|
Owned mutable wrapper for platform-native strings, used to make platform API calls and interoperate with "C" code. |
|
Borrowed reference to OsString |
|
Owned mutable filesystem path, adds methods for interacting with the Rust filesystem |
|
Borrowed reference to PathBuf slice |