How to Convert a String into a Vector of Bytes in Rust

If you are working with Rust and need to convert a string into a vector of bytes, there are a couple of ways to achieve this. In this article, we will explore the different methods available and provide examples to help you understand the solutions.

Method 1: Using .as_bytes()

The first method involves using the .as_bytes() function, which gives you a view of a string as a &[u8] byte slice. This method can be called on a String since it dereferences to str. Here’s an example:

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

In the above code, string.as_bytes() returns a byte slice representing the ASCII values of the characters in the string “foo”. The output [102, 111, 111] corresponds to the ASCII values of the characters ‘f’, ‘o’, and ‘o’ respectively.

Method 2: Using .into_bytes()

The second method involves using the .into_bytes() function, which consumes a String and gives you a Vec<u8>. This method is useful when you need ownership of the bytes. Here’s an example:

fn main() {
    let string = String::from("foo");
    let bytes: Vec<u8> = string.into_bytes();
    println!("{:?}", bytes); // prints [102, 111, 111]

In the above code, string.into_bytes() converts the String “foo” into a Vec<u8>, which stores the ASCII values of the characters ‘f’, ‘o’, and ‘o’ in the vector. The output [102, 111, 111] is the same as in the previous example.

Naming Conventions for Conversion Functions

It’s worth noting that Rust’s naming conventions for conversion functions are helpful in situations like these. The .as_bytes() and .into_bytes() functions provide clear indications of their purpose, allowing you to easily identify the appropriate method to use.


In this article, we explored two methods for converting a string into a vector of bytes in Rust. The .as_bytes() function provides a view of the string as a byte slice, while the .into_bytes() function consumes the string and returns a vector of bytes. By understanding these methods and their differences, you can effectively convert strings into vectors of bytes in your Rust programs.

Related Posts

How to Post an Empty Body to REST API via HttpClient in C When working with REST APIs in C#, you may come across scenarios where you need to send a POST request with an empty body. This can be useful in situations where the API endpoint expects a POST request but doesn’t require any ...

Read more

Does C# have IsNullOrEmpty for List/IEnumerable?

If you’ve been working with C# for a while, you may have come across situations where you need to check if a List or IEnumerable is null or empty. In some other languages, like JavaScript, you have the convenience of using the Array.isArray() method or the length property to check if an array is empty ...

Read more

List Queries: 20 Times Faster than IQueryable?

Have you ever wondered why list queries are often faster than IQueryable queries in C#? In this article, we will explore the reasons behind this performance difference and discuss when and how to use each type of query. What is IQueryable? Before we dive into the performance comparison, let’s first understand what IQueryable is. IQueryable ...

Read more

Passing Different Number of Arguments to a Params Method at Runtime

How to Pass a Different Number of Arguments Using a List and Converting to an Array One way to pass a different number of arguments to a params method at runtime is by using a List<object> to store the arguments and then converting it to an array using the ToArray() method. Here’s an example: List<object> ...

Read more

Leave a Comment