Index

Rust

  1. Guessing Game
  2. Common Programming Concepts
    1. Variables and Mutability
    2. Data Types
    3. Function
    4. Control Flow
  3. Understanding Ownership
    1. References and Borrowing
    2. The Slice Type
  4. Using Structs
    1. An Example Program Using Structs
    2. Method Syntax
  5. Enums and Pattern Matching
    1. The match Control Flow Operator
    2. Concise Control Flow with if let
  6. Managing Growing Projects with Packages, Crates, and Modules
    1. Defining Modules to Control Scope and Privacy
    2. Paths for Referring to an Item in the Module Tree
    3. Bringing Paths into Scope with the use Keyword
    4. Separating Modules into Different Files
  7. Common Collections
    1. Storing UTF-8 Encoded Text with Strings
    2. Storing Keys with Associated Values in Hash Maps
  8. Error Handling
    1. Unrecoverable Errors with panic!
    2. Recoverable Errors with Result
  9. Generic Types, Traits, and Lifetimes
    1. Traits: Defining Shared Behavior
    2. Generics Rust by Example
      1. Functions
      2. Implementation
  10. Writing Automated Tests
  11. Object Oriented Programming
  12. Adding dependancies
  13. Option Take
  14. RefCell
  15. mem
  16. Data Structure
    1. Linked List
    2. Binary search tree
    3. N-ary Sum tree
  17. Recipe
    1. Semi colon
    2. Calling rust from python
    3. Default
    4. Crytocurrency With rust
    5. Function chaining
    6. Question Mark Operator
    7. Tests with println
    8. lib and bin
    9. Append vector to hash map
    10. Random Number
    11. uuid4
    12. uwrap and option
  18. Blockchain with Rust
  19. Near Protocol
    1. Startup code
    2. Couter
    3. Status
    4. Avrit
  20. 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 = [0u816];
        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 = [0u816];
    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<[u832], getrandom::Error> {
    let mut buf = [0u832];
    getrandom::getrandom(&mut buf)?;
    Ok(buf)
}



fn get_random_buf() -> Result<[u832], getrandom::Error> {
    let mut buf = [0u832]; // [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 = [0u832];
    let mut rng: StdRng = SeedableRng::from_seed(seed);
    let mut bytes = [0u816];
    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);
}



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] = [032];
    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 = [0u816];
    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 = [0u832];
    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 = [0u816];
    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