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
- Regex example to match multiline string in Rust?
- How to extend a Rust HashMap?
- How to match whitespace with a regex in Rust?
- How to match a URL with a regex in Rust?
- How to use regex lookbehind in Rust?
- How to compare two Rust HashMaps?
- How to replace a capture group using Rust regex?
- How to use non-capturing groups in Rust regex?
- How to replace strings using Rust regex?
- How to split a string with Rust regex?
See more codes...