Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rusty-bender committed Mar 12, 2021
0 parents commit 6a7de6a
Show file tree
Hide file tree
Showing 60 changed files with 1,324 additions and 0 deletions.
6 changes: 6 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
learn how to unit test
integration tests
how to work with the file system
how to make a lambda/anonymous function
traits
generics
1 change: 1 addition & 0 deletions collections/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
5 changes: 5 additions & 0 deletions collections/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions collections/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "collections"
version = "0.1.0"
authors = ["unix"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
127 changes: 127 additions & 0 deletions collections/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
use std::collections::hash_map::HashMap;

fn main() {
let mut v = vec![1,2,3];

println!("2nd element in vector/heap array: {}", &v[1]);

v[1] = 7;

println!("2nd element in vector/heap array: {}", &v[1]);

// println!("7th element does not exist and will panic: {}", &v[7]);
println!("7th element in vector/heap array: {:?}", v.get(7)); // returns an Option enum instead of panic

match v.get(7) {
Some(x) => println!("found a value: {}", x),
None => println!("Couldn't find anything")
}

v.push(73); // add an array element

println!("array now looks like: {:?}", v);

for i in &v {
println!("{}", i);
}

for i in &mut v {
*i += 1;
println!("{}", i);
}

let mut pickles = vec![Pickle{tasty:3}];

for p in &mut pickles {
p.tasty = 7; // equivalent to the next line
(*p).tasty = 7; // don't need to dereference this, rust will do it automatically
}

println!("pickles array now looks like: {:#?}", pickles);


let mut ints: Vec<i32> = Vec::new();

ints.push(1);
ints.push(10);

// Find the average of the ints
let mut total = 0;
for i in &ints {
total += i;
}

let length = ints.len();
match length {
0 => println!("Average is 0"),
_ => println!("Average of ints: {}", total as usize / length)
}

// Find the mean of the ints

// First, sort the array
ints.push(-3);
ints.push(74);

ints.sort();

let length = ints.len();

println!("the median is {:?} in {:?}", ints.get(length / 2), ints);

// Find the mode of the array (the value that appears the most)

ints.push(10);
ints.push(11);
ints.push(11);
ints.push(11);

let mut map = HashMap::new();

for i in &ints {
let entry = map.entry(i).or_insert(0);
*entry += 1; // keep a count of how many times this thing shows up.
}

println!("Here's the map: {:#?}", map);

let mut max_key: i32 = 0;
let mut max_count: u32 = 0;

for (k, v) in &map {
if *v > max_count {
max_key = **k;
max_count = *v;
}
}

println!("the mode is {:?}", max_key);


// Convert strings to pig latin.
// The first consonant of each word is moved to the end of the word and “ay” is added,
// so “first” becomes “irst-fay.” Words that start with a vowel have “hay” added to
// the end instead (“apple” becomes “apple-hay”).
// Keep in mind the details about UTF-8 encoding!

let original_string = String::from("first");

println!("converted string: {}", pig_latin(&original_string));
println!("converted string: {}", pig_latin(&String::from("orange")));
}

fn pig_latin(s: &String) -> String {
let vowels = ["a","e","i","o","u"];
let first_letter = &s[0..1];

if vowels.contains(&first_letter) {
return s.clone() + "-hay";
}

String::from(&s[1..]) + "-" + first_letter + "ay"
}

#[derive(Debug)] // allows print
struct Pickle {
tasty: u32
}
1 change: 1 addition & 0 deletions enums/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
5 changes: 5 additions & 0 deletions enums/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions enums/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "enums"
version = "0.1.0"
authors = ["unix"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
123 changes: 123 additions & 0 deletions enums/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
fn main() {
let male = SexKind::Male;
println!("sex 1: {:?}", male);

let male = SexStatsKind::Male(String::from("penis"), 330, 401, 89);
println!("male w/ stats: {:?}", male);

// call a method on the enum
let message = Message::Quit;
message.do_something();


// The std lib comes with the Option enum
// so we can take care of traditional null values
// enum Option<T> {
// Some(T),
// None,
// }

// This is what the
let some_value = Option::Some("74");
let some_value = Some("74");
let no_value: Option<u32> = None;

// Now we can use the match expression to take care
// of an Option enum, making sure to take into account
// a null case

// match expression
println!("value of a dime is {}", value_in_cents(Coin::Dime));
println!("value of an Alabama quarter is {}", value_in_cents(Coin::Quarter(UsState::Alabama)));

// let's look at using option in a function definition
println!("value of 5 + 1 is {:?}", add_one(Some(5)));
println!("value of None + 1 is {:?}", add_one(None));

// if we have multiple arms that we don't need to cover we can use _
let some_u8_value = 5u8;
match some_u8_value {
1 => println!("one"),
3 => println!("three"),
5 => println!("five"),
7 => println!("seven"),
_ => (),
}

// instead of writing a match expression
// you can use if let instead
let coin = Coin::Dime;

if let Coin::Quarter(state) = coin {
println!("State quarter from {:?}!", state);
} else if let Coin::Dime = coin {
println!("picllek");
} else {
println!("No quarter...");
}
}

fn add_one(num: Option<i8>) -> Option<i8> {
match num {
None => None,
Some(i) => Some(i + 1)
}
}

#[derive(Debug)]
enum UsState {
Alabama,
Alaska,
}

enum Coin {
Penny,
Nickel,
Dime,
Quarter(UsState),
}

fn value_in_cents(coin: Coin) -> u8 {
match coin {
Coin::Penny => {
// do some stuff here
1 // return value
},
Coin::Nickel => 5,
Coin::Dime => 10,
Coin::Quarter(state) => {
println!("the quarter is from {:?}", state);
25
},
}
}

#[derive(Debug)]
enum SexKind {
Male,
Female
}

// We can associate data with the enums
// They can have different data
#[derive(Debug)]
enum SexStatsKind {
Male(String, u32, u32, u32), // You can even store a struct here (it's just a tuple of data)
Female(String, u32)
}

// another example of structs
// Grouping possibilities and associated data is powerful when we pass data to functions and back.
enum Message {
Quit,
Move { x: i32, y: i32 }, // this is an anonymous struct
Write(String),
ChangeColor(i32, i32, i32),
}

// You can even define methods on enums, just like structs
impl Message {
fn do_something(&self) {
// do something here...
}
}
1 change: 1 addition & 0 deletions errors/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
5 changes: 5 additions & 0 deletions errors/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions errors/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "errors"
version = "0.1.0"
authors = ["unix"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
1 change: 1 addition & 0 deletions errors/hello.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
123
Loading

0 comments on commit 6a7de6a

Please sign in to comment.