Skip to content

Commit a9cd67c

Browse files
author
Tim McNamara
committed
Add all source code files
1 parent c4d4c17 commit a9cd67c

File tree

380 files changed

+1322820
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

380 files changed

+1322820
-0
lines changed

celsius-0-debug.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
struct Celsius(f32);
2+
3+
fn main() {
4+
let nice_and_warm = Celsius(22.3);
5+
println!("It's {:?}℃ today.", nice_and_warm);
6+
}

celsius-0.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
struct Celsius(f32);
2+
3+
fn main() {
4+
let nice_and_warm = Celsius(22.3);
5+
println!("It's {}℃ today.", nice_and_warm);
6+
}

ch1-animals-specialization.rs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
struct Animal {
2+
age: i32,
3+
}
4+
5+
type Cat = Animal;
6+
type Dog = Animal;
7+
type LoudDog = Dog;
8+
9+
trait Talk {
10+
fn talk(&self) -> ();
11+
}
12+
13+
impl Talk for Animal {
14+
default fn talk(&self) { // note the use of the default
15+
println!("<silence>");
16+
}
17+
}
18+
19+
impl Talk for Cat {
20+
fn talk(&self) {
21+
println!("Meow");
22+
}
23+
}
24+
25+
impl Talk for Dog {
26+
fn talk(&self) {
27+
println!("Woof!");
28+
}
29+
}
30+
31+
impl Talk for LoudDog {
32+
fn talk(&self) {
33+
println!("WOOF!!");
34+
}
35+
}
36+
37+
38+
39+
fn main() {
40+
let fluffy = Cat(Animal { age: 4 });
41+
let max = Dog(Animal { age: 2 });
42+
let neighbours_dog = LoudDog(Animal { age: 7 });
43+
44+
fluffy.talk();
45+
max.talk();
46+
neighbours_dog.talk();
47+
}

ch1-animals-tuple-structs.rs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
struct Animal {
2+
age: i32,
3+
}
4+
5+
struct Cat(Animal);
6+
struct Dog(Animal);
7+
struct LoudDog(Animal);
8+
9+
trait Talk {
10+
fn talk(&self) -> ();
11+
}
12+
13+
impl Talk for Cat {
14+
fn talk(&self) {
15+
println!("Meow");
16+
}
17+
}
18+
19+
impl Talk for Dog {
20+
fn talk(&self) {
21+
println!("Woof!");
22+
}
23+
}
24+
25+
impl Talk for LoudDog {
26+
fn talk(&self) {
27+
println!("WOOF!!");
28+
}
29+
}
30+
31+
32+
33+
fn main() {
34+
let fluffy = Cat(Animal { age: 4 });
35+
let max = Dog(Animal { age: 2 });
36+
let neighbours_dog = LoudDog(Animal { age: 7 });
37+
38+
fluffy.talk();
39+
max.talk();
40+
neighbours_dog.talk();
41+
}

ch1-animals.rs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
struct Animal {
2+
age: i32,
3+
}
4+
5+
struct Cat(Animal);
6+
struct Dog(Animal);
7+
struct LoudDog(Animal);
8+
9+
trait Talk {
10+
fn talk(&self) -> ();
11+
}
12+
13+
impl Talk for Cat {
14+
fn talk(&self) {
15+
println!("Meow");
16+
}
17+
}
18+
19+
impl Talk for Dog {
20+
fn talk(&self) {
21+
println!("Woof!");
22+
}
23+
}
24+
25+
impl Talk for LoudDog {
26+
fn talk(&self) {
27+
println!("WOOF!!");
28+
}
29+
}
30+
31+
32+
33+
fn main() {
34+
let fluffy = Cat(Animal { age: 4 });
35+
let max = Dog(Animal { age: 2 });
36+
let neighbours_dog = LoudDog(Animal { age: 7 });
37+
38+
fluffy.talk();
39+
max.talk();
40+
neighbours_dog.talk();
41+
}

ch1-escape-html.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
fn escape_html(maybe_html: &str) -> String {
2+
let mut out = String::with_capacity(maybe_html.len());
3+
4+
for c in maybe_html.chars() {
5+
match c {
6+
'<' => out.push_str("&lt;"),
7+
'>' => out.push_str("&gt;"),
8+
'&' => out.push_str("&amp;"),
9+
'\'' => out.push_str("&apos;"),
10+
'"' => out.push_str("&quot;"),
11+
_ => out.push(c),
12+
};
13+
}
14+
15+
out
16+
}
17+
18+
fn main() {
19+
let html = "<p>\"Hello, World!\"</p>";
20+
let escaped_html = escape_html(html);
21+
println!("{}", escaped_html);
22+
}

ch1-hashmap-hashset.rs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use std::collections::{HashMap, HashSet};
2+
3+
fn main() {
4+
5+
let input_text = "does this work
6+
i dont know
7+
how rust works";
8+
9+
let mut character_counts = HashMap::new();
10+
11+
let mut n_lines = 0u32;
12+
13+
for l in input_text.lines() {
14+
n_lines = n_lines + 1;
15+
16+
let mut chars_for_line = HashSet::new();
17+
18+
for c in l.chars() {
19+
if chars_for_line.contains(&c) {
20+
continue
21+
}
22+
let c_count = character_counts.entry(c).or_insert(0u32);
23+
*c_count += 1;
24+
chars_for_line.insert(c);
25+
}
26+
}
27+
28+
for (c, c_count) in &character_counts {
29+
if *c_count == n_lines {
30+
println!("{}", c);
31+
}
32+
}
33+
}

ch1-hello2.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
fn greet_world() {
2+
println!("Hello, world!"); // our old friend.
3+
4+
let southern_germany = "Grüß Gott!";
5+
let japan = "ハロー・ワールド";
6+
7+
let regions = [southern_germany, japan];
8+
9+
for region in regions.iter() {
10+
println!("{}", &region);
11+
}
12+
}
13+
14+
fn main() {
15+
greet_world();
16+
}

ch1-save-user-data.rs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use std::str;
2+
3+
#[derive(Debug)]
4+
struct User {
5+
id: u8,
6+
secret: String,
7+
}
8+
9+
fn store_secrets(user: &User, buffer: &mut[u8]) {
10+
let _secret = user.secret.clone();
11+
12+
13+
// assume we're writing to a database
14+
println!("{:?}: {}", user, str::from_utf8(&buffer).unwrap());
15+
}
16+
17+
fn main() {
18+
let buffer = &mut[0u8; 1024];
19+
let u1 = User {
20+
id: 1,
21+
secret: String::from("Pa55w0rd!"),
22+
};
23+
let u2 = User {
24+
id: 2,
25+
secret: String::from("correct horse battery staple"),
26+
};
27+
28+
store_secrets(&u1, buffer);
29+
store_secrets(&u2, buffer);
30+
31+
32+
}

ch1-time-api/Cargo.toml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "ch1-time-api"
3+
version = "0.1.0"
4+
authors = ["Tim McNamara <code@timmcnamara.co.nz>"]
5+
6+
[dependencies]
7+
chrono = "0.4.0"
8+
rocket = "0.3.0"
9+
rocket_codegen = "0.3.0"
10+
serde = "1.0"
11+
serde_derive = "1.0"
12+
13+
[dependencies.rocket_contrib]
14+
version = "0.3.0"
15+
default-features = false
16+
features = ["json"]

ch1-time-api/src/main.rs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#![feature(plugin)] // <1>
2+
#![plugin(rocket_codegen)] // <1>
3+
4+
extern crate serde; // <2>
5+
extern crate chrono; // <2>
6+
extern crate rocket; // <2>
7+
extern crate rocket_contrib; // <2>
8+
9+
#[macro_use] // <3> Syntax to indicate that we want to import macros from another module
10+
extern crate serde_derive; // <3>
11+
12+
use chrono::prelude::*; // <4> brings all exported members into local scope (e.g. DateTime and Utc)
13+
use rocket_contrib::{Json}; // <5> bring single member into local scope
14+
15+
#[derive(Serialize)] // <6> Automatically generate a string representation of this struct (which will be used as JSON)
16+
struct Timestamp { // <7> Syntax to create a custom type
17+
time: String, // <8> The `Timestamp` `time` field is of type `String`
18+
}
19+
20+
#[get("/")] // <9> Custom syntax provided by the library that indicates to code generation
21+
fn index() -> &'static str { // <10> Define a function with no arguments and its return type
22+
"Hello, world!" // <11> Rust returns the result of the final expression
23+
}
24+
25+
#[get("/time")]
26+
fn time_now() -> Json<Timestamp> {
27+
let now: DateTime<Utc> = Utc::now();
28+
let timestamp = Timestamp { time: now.to_rfc3339() };
29+
Json(timestamp)
30+
}
31+
32+
fn main() {
33+
rocket::ignite()
34+
.mount("/", routes![index, time_now])
35+
.launch();
36+
}

ch1-word-counts.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use std::collections::HashMap;
2+
3+
fn main() {
4+
let text = "once upon a time ...";
5+
let mut word_counts = HashMap::new();
6+
7+
let pairs = text.split(" ")
8+
.map(|x| { (x, 1) });
9+
10+
for (word, count) in pairs {
11+
let tmp = word_counts.entry(word)
12+
.or_insert(0);
13+
*tmp += count;
14+
}
15+
println!("{:?}", word_counts);
16+
}

ch1/ch1-animals-specialization.rs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
struct Animal {
2+
age: i32,
3+
}
4+
5+
type Cat = Animal;
6+
type Dog = Animal;
7+
type LoudDog = Dog;
8+
9+
trait Talk {
10+
fn talk(&self) -> ();
11+
}
12+
13+
impl Talk for Animal {
14+
default fn talk(&self) { // note the use of the default
15+
println!("<silence>");
16+
}
17+
}
18+
19+
impl Talk for Cat {
20+
fn talk(&self) {
21+
println!("Meow");
22+
}
23+
}
24+
25+
impl Talk for Dog {
26+
fn talk(&self) {
27+
println!("Woof!");
28+
}
29+
}
30+
31+
impl Talk for LoudDog {
32+
fn talk(&self) {
33+
println!("WOOF!!");
34+
}
35+
}
36+
37+
38+
39+
fn main() {
40+
let fluffy = Cat(Animal { age: 4 });
41+
let max = Dog(Animal { age: 2 });
42+
let neighbours_dog = LoudDog(Animal { age: 7 });
43+
44+
fluffy.talk();
45+
max.talk();
46+
neighbours_dog.talk();
47+
}

0 commit comments

Comments
 (0)