Skip to content

Instantly share code, notes, and snippets.

@csknk
Forked from rust-play/playground.rs
Last active July 3, 2020 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csknk/6e4b087d485f6f20371aa9d1cda4d8c4 to your computer and use it in GitHub Desktop.
Save csknk/6e4b087d485f6f20371aa9d1cda4d8c4 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
use std::fmt;
fn main() -> Result<(), &'static str> {
let b = vec![0x64, 0x61, 0x76, 0x65];
println!("{}", b.to_hex());
Ok(())
}
trait ToHex {
fn to_hex(&self) -> String;
}
impl<T: fmt::LowerHex> ToHex for T {
fn to_hex(&self) -> String {
format!("{:x}", self)
}
}
impl ToHex for [u8] {
fn to_hex(&self) -> String {
use core::fmt::Write;
let mut ret = String::with_capacity(2 * self.len());
for ch in self {
write!(ret, "{:02X}", ch).expect("writing to string");
}
ret
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment