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 match a URL with a regex in Rust?
- How to replace a capture group using Rust regex?
- How to clear a Rust HashMap?
- Yield example in Rust
- Example of yield_now in Rust?
- How to convert a Rust slice to a fixed array?
- How to use regex to match a group in Rust?
- How to convert a Rust HashMap to a BTreeMap?
- How to yield a thread in Rust?
- How to replace strings using Rust regex?
See more codes...