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 mutableStringcalledsand 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 astrcalleds2and assigns it the value"Hello, world!".
Helpful links
More of Rust
- How to match whitespace with a regex in Rust?
- How to replace strings using Rust regex?
- How to use regex lookbehind in Rust?
- How to use regex lookahead in Rust?
- Regex example to match multiline string in Rust?
- How to make regex case insensitive in Rust?
- How to use regex to match a double quote in Rust?
- How to replace a capture group using Rust regex?
- How to use regex captures in Rust?
- How to perform matrix operations in Rust?
See more codes...