How to convert a string to a vector of bytes in Rust?
Hi! I just started learning Rust and encountered a problem converting a string to a vector of bytes. I need to send a string over the network as a byte array, but I can't figure out how to do it correctly.
I have a variable:
```rust
let my_string = "Hello, мир!";
```
I want to get `Vec` that contains the bytes of this string. I tried using `as_bytes()`, but it returns `&[u8]`, not `Vec`. Then I tried `to_string()` and `into_bytes()`, but that didn't work.
In other languages, like Python, I just write `b"hello"` or `"hello".encode()`, and it's done. In Rust, I get confused between `String`, `&str`, `Vec` and `&[u8]`.
I specifically need `Vec` because I'm going to modify the bytes before sending (add a header). Please tell me how to do it correctly.
No answers yet.
Log in to leave an answer