Start work on challenge 12.

This commit is contained in:
2022-04-27 11:23:08 -04:00
parent 994da471c4
commit eb0f8c4ede
3 changed files with 33 additions and 11 deletions

View File

@@ -1,4 +1,5 @@
#![allow(dead_code)]
use rand::Rng;
use std::fmt::Write; // need to import this trait
#[derive(PartialEq, PartialOrd, Debug)]
@@ -15,6 +16,19 @@ impl Bytes {
String::from(std::str::from_utf8(&v).unwrap())
}
pub fn random(length: usize) -> Bytes {
Bytes(
(0..length)
.map(|_| rand::thread_rng().gen_range(0..255))
.collect(),
)
}
pub fn random_range(lower: usize, upper: usize) -> Bytes {
let length: usize = rand::thread_rng().gen_range(lower..upper);
Bytes::random(length)
}
pub fn from_hex(s: &str) -> Bytes {
if s.len() % 2 != 0 {
panic!("Input string has uneven number of characters");