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
  • Save csknk/6e4b087d485f6f20371aa9d1cda4d8c4 to your computer and use it in GitHub Desktop.
Save csknk/6e4b087d485f6f20371aa9d1cda4d8c4 to your computer and use it in GitHub Desktop.

Revisions

  1. csknk renamed this gist Jul 3, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @rust-play rust-play created this gist Jul 3, 2020.
    30 changes: 30 additions & 0 deletions playground.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    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
    }
    }