How do I convert a string into a vector of bytes in rust?

Christoph

That might be the dumbest Rustlang question ever but I promise I tried my best to find the answer in the documentation or any other place on the web.

I can convert a string to a vector of bytes like this:

let bar = bytes!("some string");

Unfortunately I can't do it this way

let foo = "some string";
let bar = bytes!(foo);

Because bytes! expects a string literal.

But then, how do I get my foo converted into a vector of bytes?

huon

(&str).as_bytes gives you a view of a string as a &[u8] byte slice (that can be called on String since that derefs to str, and there's also String.into_bytes will consume a String to give you a Vec<u8>.

Use the .as_bytes version if you don't need ownership of the bytes.

fn main() {
    let string = "foo";
    println!("{:?}", string.as_bytes()); // prints [102, 111, 111]
}

BTW, The naming conventions for conversion functions are helpful in situations like these, because they allow you to know approximately what name you might be looking for.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I convert a Vector of bytes (u8) to a string

How can I convert a String into a Vector in Rust?

How do I convert a string into bytes in python?

How do I convert a B string to bytes?

How do I convert a String to an integer in Rust?

How do I convert a string to hex in Rust?

How can I convert a string of numbers to an array or vector of integers in Rust?

convert Bytes to string rust

How do I convert a string into a string of bytes in python 2.7

How do I convert an array of bytes to string with Delphi?

How do I convert a string of escape sequences to bytes?

How do I convert a bytes object to a string in python?

How do I convert a std::string containing doubles to a vector of doubles?

How do I join a char vector in Rust

How can I convert bytes to string In Tensorflow

How do I convert lists and dictionaries into bytes?

How do I convert a list of integers into bytes?

How to convert bytes string to bytes

How do I convert a list of ASCII "Bytes" in Python into Hex Bytes

How do I remove the elements of vector that occur in another vector in Rust?

How do I convert a boolean to an integer in Rust?

How do I convert a C string into a Rust string and back via FFI?

How to convert a String of strings to an Array/Vector of Strings in Rust

How do I convert a string containing one number into a vector in C++ where all the digits are the elements of vector

How do I efficiently reorder bytes of a __m256i vector (convert int32_t to uint8_t)?

How do I convert a 44 bytes long base64 string (public key) to a 32 bytes long UInt8 array?

How to covert a vector of bytes to a byte array in Rust?

How to Convert vector into json in rust

How do I convert a HashSet of Strings into a Vector?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive