Skip to content

Commit

Permalink
Incorporate MEAPv12 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim McNamara committed Jul 10, 2020
1 parent 5d0a8c3 commit 6527f36
Show file tree
Hide file tree
Showing 115 changed files with 16,443 additions and 62 deletions.
5 changes: 5 additions & 0 deletions ch1/ch1-cereals/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 ch1/ch1-cereals/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "ch1-cereals"
version = "0.1.0"
authors = ["Tim McNamara <author@rustinaction.com>"]
edition = "2018"

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

[dependencies]
13 changes: 13 additions & 0 deletions ch1/ch1-cereals/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#[derive(Debug)]
enum Cereal {
Barley, Millet, Rice,
Rye, Spelt, Wheat,
}

fn main() {
let mut grains: Vec<Cereal> = vec![];
grains.push(Cereal::Rye);
drop(grains);

println!("{:?}", grains);
}
5 changes: 5 additions & 0 deletions ch1/ch1-fruit/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 ch1/ch1-fruit/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "ch1-fruit"
version = "0.1.0"
authors = ["Tim McNamara <author@rustinaction.com>"]
edition = "2018"

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

[dependencies]
5 changes: 5 additions & 0 deletions ch1/ch1-fruit/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main() {
let fruit = vec!['🥝', '🍌', '🍇'];
let buffer_overflow = fruit[4]; // <1>
assert_eq!(buffer_overflow, '🍉') // <2>
}
5 changes: 5 additions & 0 deletions ch1/ch1-letters/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 ch1/ch1-letters/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "ch1-letters"
version = "0.1.0"
authors = ["Tim McNamara <author@rustinaction.com>"]
edition = "2018"

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

[dependencies]
10 changes: 10 additions & 0 deletions ch1/ch1-letters/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fn main() {
let mut letters = vec![
"a", "b", "b"
];

for letter in letters {
println!("{}", letter);
letters.push(letter.clone());
}
}
5 changes: 5 additions & 0 deletions ch1/ch1-penguins/Cargo.lock

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

7 changes: 7 additions & 0 deletions ch1/ch1-penguins/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "ch1-penguins"
version = "0.1.0"
authors = ["Tim McNamara <author@rustinaction.com>"]
edition = "2018"

[dependencies]
38 changes: 38 additions & 0 deletions ch1/ch1-penguins/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
fn main() { // <1> Executable projects require a main() function
let penguin_data ="\
common name,length (cm)
Little penguin,33
Yellow-eyed penguin,65
Fiordland penguin,60
Invalid,data
";

let records = penguin_data.lines();

for (i, record) in records.enumerate() {
if i == 0 || record.trim().len() == 0 { // <2> Skip header row and lines with only whitespace
continue;
}

let fields: Vec<_> = record // <4> A "Vec" type is shorthand for vector. Vectors are arrays that will dynamically expand when needed. The underscore asks the the compiler to infer the type of the vector's elements.
.split(',') // <3> Split `record` into substrings
.map(|field| field.trim()) // <4> As well as for loops, Rust programmers can use higher-order programmers when they prefer. This line trims the whitespace from every field.
.collect(); // <5> Rust will "collect" the results of an iterator into a vector.

if cfg!(debug_assertions) { // <6> When debugging is enabled, include this code block. The exclamation mark (!) indicates a macro invocation.
eprintln!("debug: {:?} -> {:?}", record, fields); // <7> eprintln! prints to standard error. The {:?} syntax requests Rust print out the default debugging representation for the two types.
}

let name = fields[0]; // <8> Rust supports indexing collections with integers

let maybe_length: Result<f32, _> = fields[1].parse(); // <9> Rust can parse strings into other types, using the type information provided on the left-hand side. This either returns a value or an error value wrapped in a "Result". The underscore requests the compiler to infer the error type itself.

if maybe_length.is_err() { // <10> Skip any invalid data.
continue;
}

let length = maybe_length.unwrap(); // <11> "Unwrap" the f32 from the Result

println!("{}, {}cm", name, length); // <12> println! prints to stdout. The {} syntax indicates that Rust should use a programmer-defined method to represent the value as a string, rather than its debug representation available with {:?}.
}
}
5 changes: 5 additions & 0 deletions ch1/ch1-race/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 ch1/ch1-race/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "ch1-race"
version = "0.1.0"
authors = ["Tim McNamara <author@rustinaction.com>"]
edition = "2018"

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

[dependencies]
10 changes: 10 additions & 0 deletions ch1/ch1-race/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::thread;

fn main() {
let mut data = 100;

thread::spawn(|| { data = 500; });
thread::spawn(|| { data = 1000; });

println!("{}", data);
}
Loading

0 comments on commit 6527f36

Please sign in to comment.