Skip to content

Latest commit

 

History

History
38 lines (30 loc) · 900 Bytes

base64.zh.md

File metadata and controls

38 lines (30 loc) · 900 Bytes

对 base64 进行编码和解码

[![base64-badge]][base64] [![cat-encoding-badge]][cat-encoding]

将字节切片编码为base64字符串,通过encode完成,还能用decode解码。

# #[macro_use]
# extern crate error_chain;
extern crate base64;

use std::str;
use base64::{encode, decode};
#
# error_chain! {
#     foreign_links {
#         Base64(base64::DecodeError);
#         Utf8Error(str::Utf8Error);
#     }
# }

fn run() -> Result<()> {
    let hello = b"hello rustaceans";
    let encoded = encode(hello);
    let decoded = decode(&encoded)?;

    println!("origin: {}", str::from_utf8(hello)?);
    println!("base64 encoded: {}", encoded);
    println!("back to origin: {}", str::from_utf8(&decoded)?);

    Ok(())
}
#
# quick_main!(run);