rustrust string bytes
A Rust String
is a UTF-8 encoded sequence of bytes. It is a collection of u8
values, and is stored as a vector of bytes (Vec<u8>
).
let s = String::from("Hello world!");
let bytes = s.as_bytes();
The output of the above code is [72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33]
.
Code explanation
let s = String::from("Hello world!");
: This creates aString
from the given string literal.let bytes = s.as_bytes();
: This creates aVec<u8>
from theString
, containing the UTF-8 encoded bytes of the string.
Helpful links
More of Rust
- How to match whitespace with a regex in Rust?
- How to use Unicode in a regex in Rust?
- How to sleep in Rust
- How to match a URL with a regex in Rust?
- How to convert a Rust slice to a fixed array?
- How to get a capture group using Rust regex?
- How to replace a capture group using Rust regex?
- How to use regex to match a double quote in Rust?
- How to use non-capturing groups in Rust regex?
- How to use regex with bytes in Rust?
See more codes...