Added final tutorial stuff

This commit is contained in:
Eduardo Cueto Mendoza 2020-07-18 21:33:40 -06:00
parent 14784cd6a1
commit ac71b3b67a
8 changed files with 218 additions and 2 deletions

18
src/cli.rs Normal file
View File

@ -0,0 +1,18 @@
use std::env;
pub fn run() {
let args: Vec<String> = env::args().collect();
let command = args[1].clone();
let name = "Brad";
let status = "100%";
// println!("Command: {}", command);
if command == "hello" {
println!("Hi {}, how are you?", name);
} else if command == "status" {
println!("Status is {}", status);
} else {
println!("This is not a valid command");
}
}

19
src/conditionals.rs Normal file
View File

@ -0,0 +1,19 @@
pub fn run() {
let age: u8 = 30;
let check_id: bool = false;
let knows_person_of_age = true;
//If/Else
if age >= 21 && check_id || knows_person_of_age {
println!("Bartender: what would you like to drink?");
} else if age < 21 && check_id {
println!("Bartender: sorry you have to leave");
} else {
println!("Bartender: I'll need to see your ID");
}
// Shorthand if
let is_of_age = if age >= 21 { true } else { false };
println!("is of age: {}", is_of_age)
}

29
src/enums.rs Normal file
View File

@ -0,0 +1,29 @@
enum Movement {
// Variants
Up,
Down,
Left,
Right
}
fn move_avatar(m: Movement) {
// Perform action depending on info
match m {
Movement::Up => println!("Avatar moving up"),
Movement::Down => println!("Avatar moving downs"),
Movement::Left => println!("Avatar moving left"),
Movement::Right => println!("Avatar moving right")
}
}
pub fn run() {
let avatar1 = Movement::Left;
let avatar2 = Movement::Up;
let avatar3 = Movement::Right;
let avatar4 = Movement::Down;
move_avatar(avatar1);
move_avatar(avatar2);
move_avatar(avatar3);
move_avatar(avatar4);
}

20
src/functions.rs Normal file
View File

@ -0,0 +1,20 @@
pub fn run() {
greeting("hello", "Jane");
// Bind function values to variables
let get_sum = add(5, 5);
println!("Sum: {}", get_sum);
// Closure
let n3: i32 = 10;
let add_nums = |n1:i32, n2:i32| n1 + n2 + n3;
println!("C Sum: {}", add_nums(3,3));
}
fn greeting(greet:&str, name:&str) {
println!("{} {}, nice to meet you!", greet, name);
}
fn add(n1:i32,n2:i32) ->i32 {
n1 + n2
}

41
src/loops.rs Normal file
View File

@ -0,0 +1,41 @@
pub fn run() {
let mut count = 0;
// Infinite loop
// loop {
// count += 1;
// println!("Number: {}", count);
// if count == 20 {
// break;
// }
// }
// While loop (FizzBuzz)
while count <= 100 {
if count % 15 == 0 {
println!("fizzbuzz");
} else if count % 3 == 0 {
println!("fizz");
} else if count % 5 == 0 {
println!("buzz");
} else {
println!("{}", count);
}
// Inc
count += 1;
}
for x in 1..100 {
if x % 15 == 0 {
println!("fizzbuzz");
} else if x % 3 == 0 {
println!("fizz");
} else if x % 5 == 0 {
println!("buzz");
} else {
println!("{}", x);
}
}
}

View File

@ -4,7 +4,14 @@
//mod strings;
//mod tuples;
//mod arrays;
mod vectors;
//mod vectors;
//mod conditionals;
//mod loops;
//mod functions;
//mod pointer_ref;
//mod structs;
// mod enums
mod cli;
fn main() {
//print::run();
@ -13,5 +20,12 @@ fn main() {
//strings::run();
//tuples::run();
//arrays::run();
vectors::run();
//vectors::run();
//conditionals::run();
//loops::run();
//functions::run();
//pointer_ref::run();
// structs::run();
// enums::run();
cli::run();
}

13
src/pointer_ref.rs Normal file
View File

@ -0,0 +1,13 @@
pub fn run() {
// Primitive Array
let arr1 = [1,2,3];
let arr2 = arr1;
// Need to use reference for non primitive values
// Vector
let vec1 = vec![1,2,3];
let vec2 = &vec1;
println!("Values: {:?}", (&vec1,vec2));
}

62
src/structs.rs Normal file
View File

@ -0,0 +1,62 @@
//traditional struct
// struct Color {
// red: u8,
// green: u8,
// blue: u8
// }
// Tuple struct
// struct Color(u8,u8,u8);
struct Person {
first_name: String,
last_name: String,
}
impl Person {
// Construct person
fn new(first: &str, last: &str) -> Person {
Person {
first_name: first.to_string(),
last_name: last.to_string()
}
}
// Get full name
fn full_name(&self) -> String {
format!("{} {}", self.first_name, self.last_name)
}
// Set last name
fn set_last_name(&mut self, last:&str) {
self.last_name = last.to_string();
}
// Name to tuple
fn to_tuple(self) -> (String,String) {
(self.first_name,self.last_name)
}
}
pub fn run() {
// let mut c = Color {
// red: 255,
// green: 255,
// blue: 255
// };
// c.red = 200;
//println!("Color: {} {} {}", c.red,c.green,c.blue);
// let mut c = Color(255,0,0);
// c.0 = 200;
// println!("Color: {} {} {}", c.0,c.1,c.2);
let mut p = Person::new("Mary", "Doe");
println!("Person {}", p.full_name());
p.set_last_name("Williams");
println!("Person {}", p.full_name());
println!("Person {:?}", p.to_tuple());
}