Rust中字符串的base64编码与解码

22-09-21 banq

将字符串编码为 base64 的程序:

use base64::encode;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>>{

   let string = b"Welcome to Linuxhint";
   let encoded = encode(string);
   println!("Base64: {}", encoded);
   Ok(())

}

解码 Base64的代码:

use base64::decode;
use std::str;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>>{

 let b64 = "V2VsY29tZSB0byBMaW51eGhpbnQ=";
 let decoded = &decode(b64).unwrap()[..];
 println!("String: {:?}", str::from_utf8(decoded));
 Ok(())

}