forked from rust-in-action/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tim McNamara
committed
Jul 10, 2020
1 parent
5d0a8c3
commit 6527f36
Showing
115 changed files
with
16,443 additions
and
62 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {:?}. | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.