Index
- Guessing Game
- Common Programming Concepts
- Understanding Ownership
- Using Structs
- Enums and Pattern Matching
- Managing Growing Projects with Packages, Crates, and Modules
- Defining Modules to Control Scope and Privacy
- Paths for Referring to an Item in the Module Tree
- Bringing Paths into Scope with the use Keyword
- Separating Modules into Different Files
- Common Collections
- Error Handling
- Generic Types, Traits, and Lifetimes
- Writing Automated Tests
- Object Oriented Programming
- Adding dependancies
- Option Take
- RefCell
- mem
- Data Structure
- Recipe
- Semi colon
- Calling rust from python
- Default
- Crytocurrency With rust
- Function chaining
- Question Mark Operator
- Tests with println
- lib and bin
- Append vector to hash map
- Random Number
- uuid4
- uwrap and option
- Blockchain with Rust
- Near Protocol
- Actix-web
uuid4
use crate::prelude::*;
impl Uuid {
/// Creates a random UUID.
///
/// This uses the [`getrandom`] crate to utilise the operating system's RNG
/// as the source of random numbers. If you'd like to use a custom generator,
/// don't use this method: use the `rand::Rand trait`'s `rand()` method instead.
///
/// Note that usage of this method requires the `v4` feature of this crate
/// to be enabled.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use uuid::Uuid;
///
/// let uuid = Uuid::new_v4();
/// ```
///
/// [`getrandom`]: https://crates.io/crates/getrandom
/// [`rand`]: https://crates.io/crates/rand
// TODO: change signature to support uuid's Error.
pub fn new_v4() -> Result<Uuid, getrandom::Error> {
let mut bytes = [0u8; 16];
getrandom::getrandom(&mut bytes)?;
let uuid = crate::builder::Builder::from_bytes(bytes)
.set_variant(Variant::RFC4122)
.set_version(Version::Random)
.build();
Ok(uuid)
}
}
#[cfg(test)]
mod tests {
use crate::prelude::*;
#[test]
fn test_new() {
let uuid = Uuid::new_v4().unwrap();
assert_eq!(uuid.get_version(), Some(Version::Random));
assert_eq!(uuid.get_variant(), Some(Variant::RFC4122));
}
#[test]
fn test_get_version() {
let uuid = Uuid::new_v4().unwrap();
assert_eq!(uuid.get_version(), Some(Version::Random));
assert_eq!(uuid.get_version_num(), 4)
}
}
impl Uuid {
/// Creates a random UUID.
///
/// This uses the [`getrandom`] crate to utilise the operating system's RNG
/// as the source of random numbers. If you'd like to use a custom generator,
/// don't use this method: use the `rand::Rand trait`'s `rand()` method instead.
///
/// Note that usage of this method requires the `v4` feature of this crate
/// to be enabled.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use uuid::Uuid;
///
/// let uuid = Uuid::new_v4();
/// ```
///
/// [`getrandom`]: https://crates.io/crates/getrandom
/// [`rand`]: https://crates.io/crates/rand
// TODO: change signature to support uuid's Error.
pub fn new_v4() -> Result<Uuid, getrandom::Error> {
let mut bytes = [0u8; 16];
getrandom::getrandom(&mut bytes)?;
let uuid = crate::builder::Builder::from_bytes(bytes)
.set_variant(Variant::RFC4122)
.set_version(Version::Random)
.build();
Ok(uuid)
}
}
#[cfg(test)]
mod tests {
use crate::prelude::*;
#[test]
fn test_new() {
let uuid = Uuid::new_v4().unwrap();
assert_eq!(uuid.get_version(), Some(Version::Random));
assert_eq!(uuid.get_variant(), Some(Variant::RFC4122));
}
#[test]
fn test_get_version() {
let uuid = Uuid::new_v4().unwrap();
assert_eq!(uuid.get_version(), Some(Version::Random));
assert_eq!(uuid.get_version_num(), 4)
}
}
use uuid::{Builder, Uuid, Variant, Version};
fn main() {
let bytes = [0u8; 16];
let uuid = Builder::from_bytes(bytes)
.set_variant(Variant::RFC4122)
.set_version(Version::Random)
.build();
println!("{:?}", uuid);
}
fn main() {
let bytes = [0u8; 16];
let uuid = Builder::from_bytes(bytes)
.set_variant(Variant::RFC4122)
.set_version(Version::Random)
.build();
println!("{:?}", uuid);
}
TL;DR version, use https://github.com/rust-random/getrandom
fn get_random_buf() -> Result<[u8; 32], getrandom::Error> {
let mut buf = [0u8; 32];
getrandom::getrandom(&mut buf)?;
Ok(buf)
}
fn get_random_buf() -> Result<[u8; 32], getrandom::Error> {
let mut buf = [0u8; 32]; // [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
getrandom::getrandom(&mut buf)?;
Ok(buf)
}
fn main() {
let data = get_random_buf();
println!("{:?}", data);
}
let mut buf = [0u8; 32]; // [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
getrandom::getrandom(&mut buf)?;
Ok(buf)
}
fn main() {
let data = get_random_buf();
println!("{:?}", data);
}
Ok([126, 92, 235, 31, 246, 57, 236, 225, 86, 14, 23, 58, 212, 8, 140, 7, 140, 79, 110, 92, 208, 180, 208, 201, 122, 9, 181, 33, 241, 82, 3, 91])
How to generate an array of random bytes from a seed?
https://stackoverflow.com/questions/62308909/how-to-generate-an-array-of-random-bytes-from-a-seed/62309203
use rand::{rngs::StdRng, RngCore, SeedableRng};
use uuid::{Builder, Variant, Version};
fn main() {
let seed = [0u8; 32];
let mut rng: StdRng = SeedableRng::from_seed(seed);
let mut bytes = [0u8; 16];
rng.fill_bytes(&mut bytes);
let uuid = Builder::from_bytes(bytes)
.set_variant(Variant::RFC4122)
.set_version(Version::Random)
.build();
println!("{:?}", uuid);
}
use uuid::{Builder, Variant, Version};
fn main() {
let seed = [0u8; 32];
let mut rng: StdRng = SeedableRng::from_seed(seed);
let mut bytes = [0u8; 16];
rng.fill_bytes(&mut bytes);
let uuid = Builder::from_bytes(bytes)
.set_variant(Variant::RFC4122)
.set_version(Version::Random)
.build();
println!("{:?}", uuid);
}
Generating seed 32 length seed for testing:
use rand::Rng;
fn main() {
let mut rng = rand::thread_rng();
let mut randvec:Vec<u8> = Vec::new();
let mut counter = 0;
let result = loop {
counter += 1;
let n1: u8 = rng.gen();
randvec.push(n1);
if counter == 32 {
break randvec;
}
};
println!("{:?}", result);
}
fn main() {
let mut rng = rand::thread_rng();
let mut randvec:Vec<u8> = Vec::new();
let mut counter = 0;
let result = loop {
counter += 1;
let n1: u8 = rng.gen();
randvec.push(n1);
if counter == 32 {
break randvec;
}
};
println!("{:?}", result);
}
Generating from seed:
use rand::{rngs::StdRng, RngCore, SeedableRng, Rng};
use uuid::{Builder, Variant, Version};
fn main() {
let mut rng = rand::thread_rng();
let mut randarray: [u8;32] = [0; 32];
let mut counter = 0;
let result = loop {
let n1: u8 = rng.gen();
randarray[counter] = n1;
counter += 1;
if counter == 32 {
break randarray;
}
};
println!("{:?}", result);
let seed = result;
let mut rng: StdRng = SeedableRng::from_seed(seed);
let mut bytes = [0u8; 16];
rng.fill_bytes(&mut bytes);
let uuid = Builder::from_bytes(bytes)
.set_variant(Variant::RFC4122)
.set_version(Version::Random)
.build();
println!("{:?}", uuid);
}
use uuid::{Builder, Variant, Version};
fn main() {
let mut rng = rand::thread_rng();
let mut randarray: [u8;32] = [0; 32];
let mut counter = 0;
let result = loop {
let n1: u8 = rng.gen();
randarray[counter] = n1;
counter += 1;
if counter == 32 {
break randarray;
}
};
println!("{:?}", result);
let seed = result;
let mut rng: StdRng = SeedableRng::from_seed(seed);
let mut bytes = [0u8; 16];
rng.fill_bytes(&mut bytes);
let uuid = Builder::from_bytes(bytes)
.set_variant(Variant::RFC4122)
.set_version(Version::Random)
.build();
println!("{:?}", uuid);
}
Getting a vect, converting to array than give it as seed:
use rand::{rngs::StdRng, RngCore, SeedableRng, Rng};
use uuid::{Builder, Variant, Version};
fn main() {
let mut rng = rand::thread_rng();
let mut randvec:Vec<u8> = Vec::new();
let mut counter = 0;
let result_vec = loop {
counter += 1;
let n1: u8 = rng.gen();
randvec.push(n1);
if counter == 32 {
break randvec;
}
};
println!("{:?}", result_vec);
// convert vector seed to array seed
let mut seed = [0u8; 32];
let mut counter = 0;
for v in result_vec.iter() {
seed[counter] = *v;
counter += 1;
}
println!("{:?}", seed);
// feed the array seed
let mut rng: StdRng = SeedableRng::from_seed(seed);
let mut bytes = [0u8; 16];
rng.fill_bytes(&mut bytes);
let uuid = Builder::from_bytes(bytes)
.set_variant(Variant::RFC4122)
.set_version(Version::Random)
.build();
println!("{:?}", uuid);
}
use uuid::{Builder, Variant, Version};
fn main() {
let mut rng = rand::thread_rng();
let mut randvec:Vec<u8> = Vec::new();
let mut counter = 0;
let result_vec = loop {
counter += 1;
let n1: u8 = rng.gen();
randvec.push(n1);
if counter == 32 {
break randvec;
}
};
println!("{:?}", result_vec);
// convert vector seed to array seed
let mut seed = [0u8; 32];
let mut counter = 0;
for v in result_vec.iter() {
seed[counter] = *v;
counter += 1;
}
println!("{:?}", seed);
// feed the array seed
let mut rng: StdRng = SeedableRng::from_seed(seed);
let mut bytes = [0u8; 16];
rng.fill_bytes(&mut bytes);
let uuid = Builder::from_bytes(bytes)
.set_variant(Variant::RFC4122)
.set_version(Version::Random)
.build();
println!("{:?}", uuid);
}
Output:
[181, 127, 162, 178, 55, 217, 237, 166, 94, 82, 24, 25, 232, 11, 245, 41, 217, 227, 197, 179, 230, 106, 189, 82, 8, 177, 146, 76, 183, 228, 96, 76]
[181, 127, 162, 178, 55, 217, 237, 166, 94, 82, 24, 25, 232, 11, 245, 41, 217, 227, 197, 179, 230, 106, 189, 82, 8, 177, 146, 76, 183, 228, 96, 76]
e2f0388b-7abc-446d-aed8-df18d499ab17