arraysstringrusttype-conversionbase64

Convert u8 array to base64 string in Rust


I have an array of u8 in Rust. How would I go about converting these to a String representing them as base64?


Solution

  • Please note that the base64::encode function has been deprecated.

    From version 0.21.0 the preferred way to achieve the same result would be

    use base64::{engine::general_purpose, Engine as _};
    
    fn main() {
        let data: Vec<u8> = vec![1,2,3,4,5];
        println!("{}", general_purpose::STANDARD.encode(&data));
    }