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
Showing
408 changed files
with
110,292 additions
and
1,313,637 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
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 |
---|---|---|
@@ -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> | ||
} |
This file was deleted.
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 |
---|---|---|
@@ -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> | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 = "hello2" | ||
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,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!("{}", ®ion); // <6> | ||
} | ||
} | ||
|
||
fn main() { | ||
greet_world(); // <7> | ||
} |
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 |
---|---|---|
@@ -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> | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -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> | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -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); | ||
} |
Oops, something went wrong.