new programm for password generation
This commit is contained in:
parent
5a945cdcb0
commit
d9f48e9fcc
9 changed files with 528 additions and 40 deletions
49
borrowing.rs
Executable file
49
borrowing.rs
Executable file
|
@ -0,0 +1,49 @@
|
|||
// ownership
|
||||
// Happy Jesus Respawn! ;D
|
||||
|
||||
|
||||
// main function
|
||||
fn main() {
|
||||
// assign vectors
|
||||
let v1 = vec![1, 2, 3];
|
||||
let v2 = vec![1, 2, 3];
|
||||
|
||||
// use functions
|
||||
let (v1, v2, answer) = foo(v1, v2);
|
||||
let answer2 = boo(&v1, &v2);
|
||||
println!("Answer is: {}", answer);
|
||||
println!("Second answer is: {}", answer2);
|
||||
|
||||
// playing with mutable references
|
||||
let mut x = 5;
|
||||
{ // needed for error free compiling
|
||||
let y = &mut x; // y borrows x here
|
||||
*y += 1; // '*' needed to access references
|
||||
}
|
||||
println!("{}", x); // tries to borrow x here -> only 1 &mut x allowed!
|
||||
|
||||
let y = &5; // the same as 'let _y = 5; let y = &_y'
|
||||
let f = Foo {x: y };
|
||||
println!("{}", f.x);
|
||||
}
|
||||
|
||||
|
||||
// functions
|
||||
fn foo(v1: Vec<i32>, v2: Vec<i32>) -> (Vec<i32>, Vec<i32>, i32) {
|
||||
// return answer // give back ownership of vectors
|
||||
(v1, v2, 42)
|
||||
}
|
||||
|
||||
//better version of 'foo' using advantage of borrowing
|
||||
fn boo(v1: &Vec<i32>, v2: &Vec<i32>) -> i32 {
|
||||
// give back answer
|
||||
42
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// struct -> way for creating more complex data types
|
||||
struct Foo<'a> { // declaring lifetime 'a
|
||||
x: &'a i32, // using lifetime 'a
|
||||
}
|
35
enum.rs
Executable file
35
enum.rs
Executable file
|
@ -0,0 +1,35 @@
|
|||
// enum
|
||||
//
|
||||
|
||||
|
||||
// main function
|
||||
fn main() {
|
||||
// use enums
|
||||
let x: Message = Message::Move { x: 3, y: 4 };
|
||||
let y: BoardGameTurn = BoardGameTurn::Move { squares: 1 };
|
||||
|
||||
// enum working like function
|
||||
let m = Message::Write("Hello World".to_string());
|
||||
let x = foo("Hello World".to_string()); // same as above, using function 'foo()'
|
||||
|
||||
// convert vector of strings into vector of Message::Write
|
||||
let v = vec!["Hello".to_string(), "World".to_string()];
|
||||
let v1: Vec<Message> = v.into_iter().map(Message::Write).collect();
|
||||
}
|
||||
|
||||
// enmus
|
||||
enum Message {
|
||||
Quit,
|
||||
ChangeColor(i32, i32, i32), // tulpe
|
||||
Move { x: i32, y: i32 }, // struct
|
||||
Write(String),
|
||||
}
|
||||
enum BoardGameTurn {
|
||||
Move { squares: i32 },
|
||||
Pass,
|
||||
}
|
||||
|
||||
//functions
|
||||
fn foo(x: String) -> Message {
|
||||
Message::Write(x)
|
||||
}
|
105
main.rs
105
main.rs
|
@ -1,51 +1,76 @@
|
|||
// vectors
|
||||
// jesus respawn is near!
|
||||
#![allow(unstable)] // allow unstable libraries
|
||||
|
||||
|
||||
// programm for password generation
|
||||
|
||||
|
||||
// To-Do:
|
||||
// parameters for password options
|
||||
// Strength of password (number from parameter)
|
||||
// not to complicated character (| l I ö 0)
|
||||
|
||||
|
||||
// crates
|
||||
extern crate rand;
|
||||
extern crate getopts;
|
||||
|
||||
|
||||
use rand::Rng;
|
||||
// use std::__rand::thread_rng;
|
||||
use getopts::Options;
|
||||
use std::string::String;
|
||||
use std::str::FromStr;
|
||||
use std::env; // use command line arguments
|
||||
|
||||
|
||||
// main function
|
||||
fn main() {
|
||||
// assigning vectors
|
||||
let mut v = vec![1, 2, 3, 4, 5]; // v: vec<i32>
|
||||
let w = vec![10; 0]; // vector of 10 zeroes
|
||||
|
||||
// print particular element of v
|
||||
println!("the 3. element of v is:{}", v[2]); // counting elements beginning at 0
|
||||
|
||||
// index
|
||||
let i: usize = 0;
|
||||
// let j: i32 = 0; // doesn't work!
|
||||
println!("ite element: {}", v[i]);
|
||||
// get parameters
|
||||
let args: Vec<String> = env::args().collect();
|
||||
// let program = args[0].clone(); // name of program inside parameters
|
||||
|
||||
// handling out of bound errors
|
||||
match w.get(8) { // use 'get()' or 'get_mut()'
|
||||
Some(x) => println!("item 7 is: {}", x),
|
||||
None => println!("Sorry, this vectori is to short!")
|
||||
}
|
||||
|
||||
// iteration of vectors
|
||||
for i in &v { // using unmutable references
|
||||
println!("A reference to {}", i);
|
||||
}
|
||||
for i in &mut v {
|
||||
println!("A mutable reference to {}", i);
|
||||
}
|
||||
for i in v { // note: you cannot use vector again!
|
||||
println!("Take ownership of the vector and its elements {}", i);
|
||||
}
|
||||
let mut opts = Options::new(); // create new options objetct
|
||||
opts.optopt("l", "", "set length of password", "LENGTH");
|
||||
opts.optflag("h", "help", "print this help menu");
|
||||
let matches = match opts.parse(&args[1..]) {
|
||||
Ok(m) => { m } // match
|
||||
Err(f) => { panic!(f.to_string()) }
|
||||
};
|
||||
|
||||
let a = 5; // a: i32
|
||||
let b = true; // b: bool
|
||||
let a2 = double(a);
|
||||
let b2 = change_truth(b);
|
||||
println!("{}", a); // works, because i32 has no pointer -> copy trait implemented
|
||||
println!("{}", b); // works -> bool has copy trait
|
||||
|
||||
// variable for length of password
|
||||
let mut pass_length: i32 = 8;
|
||||
let mut password = String::new(); // assign password as empty string
|
||||
let mut rng = rand::thread_rng(); // instance of thread; rng = random number generator (object)
|
||||
|
||||
// variable for strength of password
|
||||
let mut score: i32 = 0; // 0 -> no password/very bad password
|
||||
|
||||
// assign array with (ascii) signs
|
||||
let mut signs : Vec<char> = vec![];
|
||||
|
||||
// fill vector with number of ASCII signs allowed for password
|
||||
for i in 33u8..126u8 {
|
||||
signs.push(i as char);
|
||||
}
|
||||
|
||||
// looping -> creation of password
|
||||
for _ in 0..pass_length {
|
||||
// choose random element from vector signs -> get ASCII signs for number
|
||||
password.push(*(rng.choose(&signs).unwrap())); // * for dereference for processing
|
||||
}
|
||||
|
||||
// length, special character, different characters, non redundance
|
||||
let length = password.len(); // get length of string
|
||||
// println!("length: {}", password.len());
|
||||
|
||||
// we have a password
|
||||
println!("password is: {}", password);
|
||||
}
|
||||
|
||||
|
||||
// function
|
||||
fn double(x: i32) -> i32 {
|
||||
x * 2
|
||||
}
|
||||
fn change_truth(x: bool) -> bool {
|
||||
!x
|
||||
fn print_usage(program: &str, opts: Options) {
|
||||
let brief = format!("Usage: {} FILE [options]", program);
|
||||
print!("{}", opts.usage(&brief));
|
||||
}
|
||||
|
|
67
match.rs
Executable file
67
match.rs
Executable file
|
@ -0,0 +1,67 @@
|
|||
// match
|
||||
//
|
||||
|
||||
|
||||
// main function
|
||||
fn main() {
|
||||
let x = 2;
|
||||
let a = 1;
|
||||
|
||||
match a {
|
||||
b => println!("a: {} b: {}", a, b), // catches '_' 'any case'
|
||||
}
|
||||
|
||||
match x { // more powerful than if/else
|
||||
1 => println!("one"), //
|
||||
2 => println!("two"),
|
||||
3 => println!("three"),
|
||||
4 => println!("four"),
|
||||
_ => println!("something else"), // ‘exhaustiveness checking’ -> error if not used
|
||||
}
|
||||
|
||||
let number = match x { // use match for variable bindings
|
||||
1 => "one", // integer converted into string
|
||||
2 => "two",
|
||||
3 => "three",
|
||||
4 => "four",
|
||||
5 => "five",
|
||||
_ => "something else",
|
||||
};
|
||||
|
||||
println!("number is: {}", number);
|
||||
|
||||
struct Point {
|
||||
x: i32,
|
||||
y: i32,
|
||||
}
|
||||
|
||||
let origin = Point { x: 0, y: 0 }
|
||||
|
||||
match origin { // deconstruct compound data type
|
||||
Point { x, y } => println!("({},{})", x, y),
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// enmus
|
||||
enum Message {
|
||||
Quit,
|
||||
ChangeColor(i32, i32, i32), // tulpe
|
||||
Move { x: i32, y: i32 }, // struct
|
||||
Write(String),
|
||||
}
|
||||
|
||||
// funtions
|
||||
fn quit() { /* ... */ }
|
||||
fn change_color(r: i32, g: i32, b: i32) { /* ... */ }
|
||||
fn move_cursor(x: i32, y: i32) { /* ... */ }
|
||||
|
||||
// process possible variants of enum with match
|
||||
fn process_message(msg: Message) {
|
||||
match msg {
|
||||
Message::Quit => quit(),
|
||||
Message::ChangeColor(r, g, b) => change_color(r, g, b),
|
||||
Message::Move { x, y: new_name_for_y } => move_cursor(x, new_name_for_y),
|
||||
Message::Write(s) => println!("{}", s),
|
||||
};
|
||||
}
|
78
passwordgenerator.rs
Executable file
78
passwordgenerator.rs
Executable file
|
@ -0,0 +1,78 @@
|
|||
#![allow(unstable)] // allow unstable libraries
|
||||
|
||||
|
||||
// programm for password generation
|
||||
|
||||
|
||||
extern crate rand;
|
||||
use rand::Rng;
|
||||
// use std::__rand::thread_rng;
|
||||
|
||||
// main function
|
||||
fn main() {
|
||||
|
||||
// variable for length of password
|
||||
let mut pass_length: i32 = 8;
|
||||
let mut password = String::new(); // assign password as empty string
|
||||
let mut rng = rand::thread_rng(); // instance of thread; rng = random number generator (object)
|
||||
|
||||
// variable for strength of password
|
||||
|
||||
// length, special character, different characters, non redundance
|
||||
|
||||
|
||||
// assign array with (ascii) signs
|
||||
let mut signs : Vec<char> = vec![];
|
||||
|
||||
// fill vector
|
||||
for i in 33u8..126u8 {
|
||||
signs.push(i as char);
|
||||
}
|
||||
|
||||
// looping -> creation of password
|
||||
for _ in 0..pass_length {
|
||||
//password.push(rng.choose(&signs).unwrap()); // get only password
|
||||
password.push(*(rng.choose(&signs).unwrap())); // * for derefernces for processing
|
||||
}
|
||||
|
||||
// we have a password
|
||||
println!("password is: {}", password);
|
||||
|
||||
// create random number between (range array elements)
|
||||
|
||||
// from number of array element -> password
|
||||
|
||||
|
||||
// create password
|
||||
// let s = rand::thread_rng()
|
||||
// .gen_ascii_chars() // generate ASCII signs
|
||||
// .take(pass_length) // take signs for password
|
||||
// .collect::<String>();
|
||||
|
||||
// print out password
|
||||
// println!("random string: {}", s);
|
||||
|
||||
// assign variable for password
|
||||
// let mut str = String::new();
|
||||
|
||||
// create password
|
||||
// for _ in () {
|
||||
// str.push(rand::random::<u8>() as char); // push character to string str
|
||||
// }
|
||||
|
||||
// assing counter
|
||||
// let mut i: i32 = 0;
|
||||
|
||||
// for (i = 0, i < pass_length, i++) {
|
||||
// str.push(rand::random::<u8>() as char);
|
||||
// }
|
||||
|
||||
// let choices = [33..126];
|
||||
// let mut rstr = String::new();
|
||||
// let mut rng = rand::thread_rng();
|
||||
|
||||
// for _ in 0..8 {
|
||||
// rstr.push((rng.choose(&choices).unwrap() as u8) as char); // Return a random element from values
|
||||
// }
|
||||
|
||||
}
|
44
pattern.rs
Executable file
44
pattern.rs
Executable file
|
@ -0,0 +1,44 @@
|
|||
// patterns
|
||||
// f*up life..
|
||||
|
||||
|
||||
// main function
|
||||
fn main() {
|
||||
// assign tuple
|
||||
let tuple: (u32, String) = (5, String::from("five"));
|
||||
|
||||
// move string -> move tiple
|
||||
let (x, _s) = tuple;
|
||||
|
||||
// gives error because tuple is moved
|
||||
// println!("Tuple is: {:?}", tuple);
|
||||
|
||||
let tuple = (5, String::from("five"));
|
||||
|
||||
// tuple is not moved -> u32 is a copy
|
||||
let (x, _) = tuple;
|
||||
|
||||
// works
|
||||
println!("Tuple is: {:?}", tuple);
|
||||
|
||||
let x = 1;
|
||||
|
||||
match x { // bind values to names
|
||||
e @ 1 ... 5 => println!("got a range element {}", e),
|
||||
_ => println!("anything"),
|
||||
}
|
||||
|
||||
#[derive(Debug)] // compiler -> basic implementations for some traits via the #[derive] attribute, traits can manually implemented
|
||||
struct Person {
|
||||
name: Option<String>,
|
||||
}
|
||||
|
||||
let name = "Steve".to_string();
|
||||
let x: Option<Person> = Some(Person { name: Some(name) };
|
||||
|
||||
match x {
|
||||
Some(Person { name: ref a @ Some(_), .. }) => println!("{:?}", a),
|
||||
_ => {}
|
||||
}
|
||||
|
||||
}
|
79
struct.rs
Executable file
79
struct.rs
Executable file
|
@ -0,0 +1,79 @@
|
|||
// struct
|
||||
// snowy eastern oO
|
||||
|
||||
|
||||
// main function
|
||||
fn main() {
|
||||
// creating complex data type
|
||||
let origin_x = 0;
|
||||
let origin_y = 0;
|
||||
|
||||
let origin = Point { x: 0, y: 0 }; // origin: Point
|
||||
|
||||
println!("The origin is at ({}, {})", origin.x, origin.y);
|
||||
|
||||
let mut point = Point { x: 0, y: 0 };
|
||||
point.x = 5; // excess field by name
|
||||
|
||||
println!("The point is at ({}, {})", point.x, point.y);
|
||||
|
||||
{
|
||||
let r = PointRef { x: &mut point.x, y: &mut point.y };
|
||||
|
||||
*r.x = 5;
|
||||
*r.y = 6;
|
||||
}
|
||||
|
||||
assert_eq!(5, point.x); // 'assert_eq' -> assert two expression are equal
|
||||
assert_eq!(6, point.y);
|
||||
|
||||
let mut point3d = Point3d { x: 0, y: 0, z: 0 };
|
||||
// point3d = { x: 1, .. point3d }; // gives point a new y, but keeps x and z // doesn't work anymore
|
||||
|
||||
|
||||
// making new point
|
||||
// let point_new = Point3d { z: 1, x: 2, .. origin };
|
||||
|
||||
// assigning color black (tuple struct)
|
||||
let black = Color(0, 0, 0); // not the same type as 'let origin = Point(0, 0, 0)'
|
||||
let origin = Point2(0, 0, 0);
|
||||
|
||||
// tuple struct with only one element -> 'newtype'
|
||||
let length = Inches(10);
|
||||
|
||||
let Inches(inter_length) = length; // extract inner integer typ
|
||||
let inter_length2 = length.0; // does the same as aboves
|
||||
|
||||
println!("length is {} inches", inter_length);
|
||||
}
|
||||
|
||||
// combine two single data types into one
|
||||
struct Point { // struct for creatiting 2D point
|
||||
x: i32, // could use 'let' here
|
||||
y: i32,
|
||||
}
|
||||
|
||||
// structure with reference pointer
|
||||
struct PointRef<'a> {
|
||||
x: &'a mut i32,
|
||||
y: &'a mut i32,
|
||||
}
|
||||
|
||||
// structure for 3D points
|
||||
struct Point3d {
|
||||
x: i32,
|
||||
y: i32,
|
||||
z: i32,
|
||||
}
|
||||
|
||||
// tuple struct
|
||||
//struct Color(i32, i32, i32);
|
||||
struct Point2(i32, i32, i32);
|
||||
struct Inches(i32);
|
||||
|
||||
// struct -> clearer
|
||||
struct Color {
|
||||
red: i32, // actually names instead positions
|
||||
blue: i32,
|
||||
green: i32,
|
||||
}
|
60
synthax_semantics.rs
Executable file
60
synthax_semantics.rs
Executable file
|
@ -0,0 +1,60 @@
|
|||
// here plays the music!
|
||||
// Syntax & Semantic
|
||||
|
||||
|
||||
// main function
|
||||
fn main() {
|
||||
let x: i32 = 5; // set x unmutable, integer 5
|
||||
|
||||
let mut y = 7; // set y mutable 5 (here an integer)
|
||||
println!("The value of y is: {}", y);
|
||||
y = 4; // change y
|
||||
println!("The value of y is: {}", y);
|
||||
|
||||
{
|
||||
println!("The value of x is: {}", x);
|
||||
let x: i32 = 7; // changing x, only inside loop
|
||||
println!("The value of x is: {}", x);
|
||||
}
|
||||
let y = "I'm a text now!"; // change y again (here as text)
|
||||
println!("The value of y is: {} ", y);
|
||||
|
||||
println!("The value of x is: {}", x);
|
||||
|
||||
print_number(88); // call function
|
||||
sum(3, 5);
|
||||
let a = add_one(x);
|
||||
|
||||
let z = (x + 2);
|
||||
println!("New values a: {}, z: {}", a, z);
|
||||
|
||||
// variable bindings with point to function
|
||||
let f: fn(i32) -> i32 = add_one; // without type inference
|
||||
//let f = add_one; // with type inference
|
||||
let six = f(5); // call function
|
||||
println!("called function says: {}", six);
|
||||
}
|
||||
|
||||
|
||||
// declaring function
|
||||
|
||||
// function for printing numbers
|
||||
fn print_number(x: i32) {
|
||||
println!("x is: {}", x);
|
||||
}
|
||||
|
||||
// function for adding two numbers
|
||||
fn sum(x: i32, y: i32) {
|
||||
println!("sum is: {}", x + y); // print sum of numbers
|
||||
}
|
||||
|
||||
// adding 1 to number
|
||||
fn add_one(i: i32) -> i32 { // -> i32 declairs return of function
|
||||
i + 1 // here no ';' -> Rust = expression based language
|
||||
// x + 1; doesn't retourn value
|
||||
}
|
||||
|
||||
// never returning function
|
||||
fn diverges() -> ! {
|
||||
panic!("This function never returns!"); // macro -> causes the current thread of execution to crash
|
||||
}
|
51
vectors.rs
Executable file
51
vectors.rs
Executable file
|
@ -0,0 +1,51 @@
|
|||
// vectors
|
||||
// jesus respawn is near!
|
||||
|
||||
|
||||
// main function
|
||||
fn main() {
|
||||
// assigning vectors
|
||||
let mut v = vec![1, 2, 3, 4, 5]; // v: vec<i32>
|
||||
let w = vec![10; 0]; // vector of 10 zeroes
|
||||
|
||||
// print particular element of v
|
||||
println!("the 3. element of v is:{}", v[2]); // counting elements beginning at 0
|
||||
|
||||
// index
|
||||
let i: usize = 0;
|
||||
// let j: i32 = 0; // doesn't work!
|
||||
println!("ite element: {}", v[i]);
|
||||
|
||||
// handling out of bound errors
|
||||
match w.get(8) { // use 'get()' or 'get_mut()'
|
||||
Some(x) => println!("item 7 is: {}", x),
|
||||
None => println!("Sorry, this vectori is to short!")
|
||||
}
|
||||
|
||||
// iteration of vectors
|
||||
for i in &v { // using unmutable references
|
||||
println!("A reference to {}", i);
|
||||
}
|
||||
for i in &mut v {
|
||||
println!("A mutable reference to {}", i);
|
||||
}
|
||||
for i in v { // note: you cannot use vector again!
|
||||
println!("Take ownership of the vector and its elements {}", i);
|
||||
}
|
||||
|
||||
let a = 5; // a: i32
|
||||
let b = true; // b: bool
|
||||
let a2 = double(a);
|
||||
let b2 = change_truth(b);
|
||||
println!("{}", a); // works, because i32 has no pointer -> copy trait implemented
|
||||
println!("{}", b); // works -> bool has copy trait
|
||||
}
|
||||
|
||||
|
||||
// function
|
||||
fn double(x: i32) -> i32 {
|
||||
x * 2
|
||||
}
|
||||
fn change_truth(x: bool) -> bool {
|
||||
!x
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue