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
Feb 16, 2020
1 parent
c4d4c17
commit a9cd67c
Showing
380 changed files
with
1,322,820 additions
and
0 deletions.
There are no files selected for viewing
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,6 @@ | ||
struct Celsius(f32); | ||
|
||
fn main() { | ||
let nice_and_warm = Celsius(22.3); | ||
println!("It's {:?}℃ today.", nice_and_warm); | ||
} |
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,6 @@ | ||
struct Celsius(f32); | ||
|
||
fn main() { | ||
let nice_and_warm = Celsius(22.3); | ||
println!("It's {}℃ today.", nice_and_warm); | ||
} |
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,47 @@ | ||
struct Animal { | ||
age: i32, | ||
} | ||
|
||
type Cat = Animal; | ||
type Dog = Animal; | ||
type LoudDog = Dog; | ||
|
||
trait Talk { | ||
fn talk(&self) -> (); | ||
} | ||
|
||
impl Talk for Animal { | ||
default fn talk(&self) { // note the use of the default | ||
println!("<silence>"); | ||
} | ||
} | ||
|
||
impl Talk for Cat { | ||
fn talk(&self) { | ||
println!("Meow"); | ||
} | ||
} | ||
|
||
impl Talk for Dog { | ||
fn talk(&self) { | ||
println!("Woof!"); | ||
} | ||
} | ||
|
||
impl Talk for LoudDog { | ||
fn talk(&self) { | ||
println!("WOOF!!"); | ||
} | ||
} | ||
|
||
|
||
|
||
fn main() { | ||
let fluffy = Cat(Animal { age: 4 }); | ||
let max = Dog(Animal { age: 2 }); | ||
let neighbours_dog = LoudDog(Animal { age: 7 }); | ||
|
||
fluffy.talk(); | ||
max.talk(); | ||
neighbours_dog.talk(); | ||
} |
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,41 @@ | ||
struct Animal { | ||
age: i32, | ||
} | ||
|
||
struct Cat(Animal); | ||
struct Dog(Animal); | ||
struct LoudDog(Animal); | ||
|
||
trait Talk { | ||
fn talk(&self) -> (); | ||
} | ||
|
||
impl Talk for Cat { | ||
fn talk(&self) { | ||
println!("Meow"); | ||
} | ||
} | ||
|
||
impl Talk for Dog { | ||
fn talk(&self) { | ||
println!("Woof!"); | ||
} | ||
} | ||
|
||
impl Talk for LoudDog { | ||
fn talk(&self) { | ||
println!("WOOF!!"); | ||
} | ||
} | ||
|
||
|
||
|
||
fn main() { | ||
let fluffy = Cat(Animal { age: 4 }); | ||
let max = Dog(Animal { age: 2 }); | ||
let neighbours_dog = LoudDog(Animal { age: 7 }); | ||
|
||
fluffy.talk(); | ||
max.talk(); | ||
neighbours_dog.talk(); | ||
} |
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,41 @@ | ||
struct Animal { | ||
age: i32, | ||
} | ||
|
||
struct Cat(Animal); | ||
struct Dog(Animal); | ||
struct LoudDog(Animal); | ||
|
||
trait Talk { | ||
fn talk(&self) -> (); | ||
} | ||
|
||
impl Talk for Cat { | ||
fn talk(&self) { | ||
println!("Meow"); | ||
} | ||
} | ||
|
||
impl Talk for Dog { | ||
fn talk(&self) { | ||
println!("Woof!"); | ||
} | ||
} | ||
|
||
impl Talk for LoudDog { | ||
fn talk(&self) { | ||
println!("WOOF!!"); | ||
} | ||
} | ||
|
||
|
||
|
||
fn main() { | ||
let fluffy = Cat(Animal { age: 4 }); | ||
let max = Dog(Animal { age: 2 }); | ||
let neighbours_dog = LoudDog(Animal { age: 7 }); | ||
|
||
fluffy.talk(); | ||
max.talk(); | ||
neighbours_dog.talk(); | ||
} |
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,22 @@ | ||
fn escape_html(maybe_html: &str) -> String { | ||
let mut out = String::with_capacity(maybe_html.len()); | ||
|
||
for c in maybe_html.chars() { | ||
match c { | ||
'<' => out.push_str("<"), | ||
'>' => out.push_str(">"), | ||
'&' => out.push_str("&"), | ||
'\'' => out.push_str("'"), | ||
'"' => out.push_str("""), | ||
_ => out.push(c), | ||
}; | ||
} | ||
|
||
out | ||
} | ||
|
||
fn main() { | ||
let html = "<p>\"Hello, World!\"</p>"; | ||
let escaped_html = escape_html(html); | ||
println!("{}", escaped_html); | ||
} |
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,33 @@ | ||
use std::collections::{HashMap, HashSet}; | ||
|
||
fn main() { | ||
|
||
let input_text = "does this work | ||
i dont know | ||
how rust works"; | ||
|
||
let mut character_counts = HashMap::new(); | ||
|
||
let mut n_lines = 0u32; | ||
|
||
for l in input_text.lines() { | ||
n_lines = n_lines + 1; | ||
|
||
let mut chars_for_line = HashSet::new(); | ||
|
||
for c in l.chars() { | ||
if chars_for_line.contains(&c) { | ||
continue | ||
} | ||
let c_count = character_counts.entry(c).or_insert(0u32); | ||
*c_count += 1; | ||
chars_for_line.insert(c); | ||
} | ||
} | ||
|
||
for (c, c_count) in &character_counts { | ||
if *c_count == n_lines { | ||
println!("{}", c); | ||
} | ||
} | ||
} |
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!"); // our old friend. | ||
|
||
let southern_germany = "Grüß Gott!"; | ||
let japan = "ハロー・ワールド"; | ||
|
||
let regions = [southern_germany, japan]; | ||
|
||
for region in regions.iter() { | ||
println!("{}", ®ion); | ||
} | ||
} | ||
|
||
fn main() { | ||
greet_world(); | ||
} |
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,32 @@ | ||
use std::str; | ||
|
||
#[derive(Debug)] | ||
struct User { | ||
id: u8, | ||
secret: String, | ||
} | ||
|
||
fn store_secrets(user: &User, buffer: &mut[u8]) { | ||
let _secret = user.secret.clone(); | ||
|
||
|
||
// assume we're writing to a database | ||
println!("{:?}: {}", user, str::from_utf8(&buffer).unwrap()); | ||
} | ||
|
||
fn main() { | ||
let buffer = &mut[0u8; 1024]; | ||
let u1 = User { | ||
id: 1, | ||
secret: String::from("Pa55w0rd!"), | ||
}; | ||
let u2 = User { | ||
id: 2, | ||
secret: String::from("correct horse battery staple"), | ||
}; | ||
|
||
store_secrets(&u1, buffer); | ||
store_secrets(&u2, buffer); | ||
|
||
|
||
} |
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 @@ | ||
[package] | ||
name = "ch1-time-api" | ||
version = "0.1.0" | ||
authors = ["Tim McNamara <code@timmcnamara.co.nz>"] | ||
|
||
[dependencies] | ||
chrono = "0.4.0" | ||
rocket = "0.3.0" | ||
rocket_codegen = "0.3.0" | ||
serde = "1.0" | ||
serde_derive = "1.0" | ||
|
||
[dependencies.rocket_contrib] | ||
version = "0.3.0" | ||
default-features = false | ||
features = ["json"] |
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,36 @@ | ||
#![feature(plugin)] // <1> | ||
#![plugin(rocket_codegen)] // <1> | ||
|
||
extern crate serde; // <2> | ||
extern crate chrono; // <2> | ||
extern crate rocket; // <2> | ||
extern crate rocket_contrib; // <2> | ||
|
||
#[macro_use] // <3> Syntax to indicate that we want to import macros from another module | ||
extern crate serde_derive; // <3> | ||
|
||
use chrono::prelude::*; // <4> brings all exported members into local scope (e.g. DateTime and Utc) | ||
use rocket_contrib::{Json}; // <5> bring single member into local scope | ||
|
||
#[derive(Serialize)] // <6> Automatically generate a string representation of this struct (which will be used as JSON) | ||
struct Timestamp { // <7> Syntax to create a custom type | ||
time: String, // <8> The `Timestamp` `time` field is of type `String` | ||
} | ||
|
||
#[get("/")] // <9> Custom syntax provided by the library that indicates to code generation | ||
fn index() -> &'static str { // <10> Define a function with no arguments and its return type | ||
"Hello, world!" // <11> Rust returns the result of the final expression | ||
} | ||
|
||
#[get("/time")] | ||
fn time_now() -> Json<Timestamp> { | ||
let now: DateTime<Utc> = Utc::now(); | ||
let timestamp = Timestamp { time: now.to_rfc3339() }; | ||
Json(timestamp) | ||
} | ||
|
||
fn main() { | ||
rocket::ignite() | ||
.mount("/", routes![index, time_now]) | ||
.launch(); | ||
} |
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 @@ | ||
use std::collections::HashMap; | ||
|
||
fn main() { | ||
let text = "once upon a time ..."; | ||
let mut word_counts = HashMap::new(); | ||
|
||
let pairs = text.split(" ") | ||
.map(|x| { (x, 1) }); | ||
|
||
for (word, count) in pairs { | ||
let tmp = word_counts.entry(word) | ||
.or_insert(0); | ||
*tmp += count; | ||
} | ||
println!("{:?}", word_counts); | ||
} |
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,47 @@ | ||
struct Animal { | ||
age: i32, | ||
} | ||
|
||
type Cat = Animal; | ||
type Dog = Animal; | ||
type LoudDog = Dog; | ||
|
||
trait Talk { | ||
fn talk(&self) -> (); | ||
} | ||
|
||
impl Talk for Animal { | ||
default fn talk(&self) { // note the use of the default | ||
println!("<silence>"); | ||
} | ||
} | ||
|
||
impl Talk for Cat { | ||
fn talk(&self) { | ||
println!("Meow"); | ||
} | ||
} | ||
|
||
impl Talk for Dog { | ||
fn talk(&self) { | ||
println!("Woof!"); | ||
} | ||
} | ||
|
||
impl Talk for LoudDog { | ||
fn talk(&self) { | ||
println!("WOOF!!"); | ||
} | ||
} | ||
|
||
|
||
|
||
fn main() { | ||
let fluffy = Cat(Animal { age: 4 }); | ||
let max = Dog(Animal { age: 2 }); | ||
let neighbours_dog = LoudDog(Animal { age: 7 }); | ||
|
||
fluffy.talk(); | ||
max.talk(); | ||
neighbours_dog.talk(); | ||
} |
Oops, something went wrong.