rustrust string and str
A String
in Rust is a type of data structure that stores a collection of characters. It is a heap-allocated data structure and is growable, meaning it can be appended to. str
is a type of string literal that is statically allocated and is not growable.
Example code
let mut s = String::from("Hello");
s.push_str(", world!");
let s2 = "Hello, world!";
Output example
Hello, world!
Code explanation
let mut s = String::from("Hello");
: This line creates a mutableString
calleds
and assigns it the value"Hello"
.s.push_str(", world!");
: This line appends the string", world!"
to the end ofs
.let s2 = "Hello, world!";
: This line creates astr
calleds2
and assigns it the value"Hello, world!"
.
Helpful links
More of Rust
- How to use regex to match a double quote in Rust?
- How to use regex to match a group in Rust?
- How to parse JSON string in Rust?
- How to use a HashBrown with a Rust HashMap?
- How to replace a capture group using Rust regex?
- How to replace all matches using Rust regex?
- How to convert a Rust slice to a fixed array?
- How to map an array in Rust
- How to split a string with Rust regex?
- How to use non-capturing groups in Rust regex?
See more codes...