Skip to content

Commit

Permalink
First edition released!
Browse files Browse the repository at this point in the history
  • Loading branch information
timClicks committed Jul 22, 2021
1 parent e23c70e commit 7d7955e
Show file tree
Hide file tree
Showing 408 changed files with 110,292 additions and 1,313,637 deletions.
47 changes: 0 additions & 47 deletions ch1/ch1-animals-specialization.rs

This file was deleted.

41 changes: 0 additions & 41 deletions ch1/ch1-animals-tuple-structs.rs

This file was deleted.

41 changes: 0 additions & 41 deletions ch1/ch1-animals.rs

This file was deleted.

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

fn main() {
let mut grains: Vec<Cereal> = vec![];
grains.push(Cereal::Rye);
drop(grains);
println!("{:?}", grains);
}
let mut grains: Vec<Cereal> = vec![]; // <3>
grains.push(Cereal::Rye); // <4>
drop(grains); // <5>

println!("{:?}", grains); // <6>
}
22 changes: 0 additions & 22 deletions ch1/ch1-escape-html.rs

This file was deleted.

8 changes: 5 additions & 3 deletions ch1/ch1-fruit/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
fn main() {
let fruit = vec!['🥝', '🍌', '🍇'];
let buffer_overflow = fruit[4]; // <1>
assert_eq!(buffer_overflow, '🍉') // <2>
}

let buffer_overflow = fruit[4]; // <1>

assert_eq!(buffer_overflow, '🍉') // <2>
}
33 changes: 0 additions & 33 deletions ch1/ch1-hashmap-hashset.rs

This file was deleted.

16 changes: 0 additions & 16 deletions ch1/ch1-hello2.rs

This file was deleted.

3 changes: 1 addition & 2 deletions ch10/untitled/Cargo.lock → ch1/ch1-hello2/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-hello2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "hello2"
version = "0.1.0"
authors = ["Tim McNamara <author@rustinaction.com>"]
edition = "2018"

[dependencies]
16 changes: 16 additions & 0 deletions ch1/ch1-hello2/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
fn greet_world() {
println!("Hello, world!"); // <1>

let southern_germany = "Grüß Gott!"; // <2>
let japan = "ハロー・ワールド"; // <3>

let regions = [southern_germany, japan]; // <4>

for region in regions.iter() { // <5>
println!("{}", &region); // <6>
}
}

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

for letter in letters {
println!("{}", letter);
letters.push(letter.clone());
letters.push(letter.clone()); // <2>
}
}
}
48 changes: 21 additions & 27 deletions ch1/ch1-penguins/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,32 @@
fn main() { // <1> Executable projects require a main() function
let penguin_data ="\
common name,length (cm)
fn main() { // <1>
let penguin_data = "\ // <2>
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;
}
if i == 0 || record.trim().len() == 0 { // <3>
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.
let fields: Vec<_> = record // <4>
.split(',') // <5>
.map(|field| field.trim()) // <6>
.collect(); // <7>

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.
}
if cfg!(debug_assertions) { // <8>
eprintln!("debug: {:?} -> {:?}",
record, fields); // <9>
}

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 {:?}.
let name = fields[0];
if let Ok(length) = fields[1].parse::<f32>() { // <10>
println!("{}, {}cm", name, length); // <11>
}
}
}
}
10 changes: 5 additions & 5 deletions ch1/ch1-race/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::thread;
use std::thread; // <1>

fn main() {
let mut data = 100;
thread::spawn(|| { data = 500; });
thread::spawn(|| { data = 1000; });

thread::spawn(|| { data = 500; }); // <2>
thread::spawn(|| { data = 1000; }); // <2>

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

0 comments on commit 7d7955e

Please sign in to comment.