diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..6686e10 --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,18 @@ +use std::env; + +pub fn run() { + let args: Vec = 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"); + } +} \ No newline at end of file diff --git a/src/conditionals.rs b/src/conditionals.rs new file mode 100644 index 0000000..a43cec2 --- /dev/null +++ b/src/conditionals.rs @@ -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) + +} \ No newline at end of file diff --git a/src/enums.rs b/src/enums.rs new file mode 100644 index 0000000..d820871 --- /dev/null +++ b/src/enums.rs @@ -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); +} \ No newline at end of file diff --git a/src/functions.rs b/src/functions.rs new file mode 100644 index 0000000..66bc294 --- /dev/null +++ b/src/functions.rs @@ -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 +} \ No newline at end of file diff --git a/src/loops.rs b/src/loops.rs new file mode 100644 index 0000000..84dcc4c --- /dev/null +++ b/src/loops.rs @@ -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); + } + } + +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 32a020d..122f3bd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(); } diff --git a/src/pointer_ref.rs b/src/pointer_ref.rs new file mode 100644 index 0000000..99a5d5b --- /dev/null +++ b/src/pointer_ref.rs @@ -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)); +} \ No newline at end of file diff --git a/src/structs.rs b/src/structs.rs new file mode 100644 index 0000000..2aef7b1 --- /dev/null +++ b/src/structs.rs @@ -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()); +} \ No newline at end of file