rustrust string as bytes
A Rust String
can be converted to a Vec<u8>
of bytes using the as_bytes()
method. This method returns a &[u8]
slice, which can be converted to a Vec<u8>
using the to_vec()
method.
Example
let my_string = String::from("Hello World!");
let bytes = my_string.as_bytes().to_vec();
Output example
[72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]
Code explanation
my_string
: aString
containing the text "Hello World!"as_bytes()
: a method that returns a&[u8]
slice of the bytes of theString
to_vec()
: a method that converts a&[u8]
slice to aVec<u8>
Helpful links
More of Rust
- How to match whitespace with a regex in Rust?
- How to split a string with 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?
- How to replace a capture group using Rust regex?
- How to match the end of a line in a Rust regex?
- How to replace all matches using Rust regex?
- How to use regex to match a group in Rust?
- How to get a capture group using Rust regex?
See more codes...