shuffle shuffle
This commit is contained in:
178
rustbook/guessing_game/Cargo.lock
generated
Normal file
178
rustbook/guessing_game/Cargo.lock
generated
Normal file
@@ -0,0 +1,178 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "0.1.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1d49d90015b3c36167a20fe2810c5cd875ad504b39cff3d4eae7977e6b7c1cb2"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
|
||||
|
||||
[[package]]
|
||||
name = "cloudabi"
|
||||
version = "0.0.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fuchsia-cprng"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
|
||||
|
||||
[[package]]
|
||||
name = "guessing_game"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"rand",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.80"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4d58d1b70b004888f764dfbf6a26a3b0342a1632d33968e4a179d8011c760614"
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.6.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"libc",
|
||||
"rand_chacha",
|
||||
"rand_core 0.4.2",
|
||||
"rand_hc",
|
||||
"rand_isaac",
|
||||
"rand_jitter",
|
||||
"rand_os",
|
||||
"rand_pcg",
|
||||
"rand_xorshift",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_chacha"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "556d3a1ca6600bfcbab7c7c91ccb085ac7fbbcd70e008a98742e7847f4f7bcef"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"rand_core 0.3.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b"
|
||||
dependencies = [
|
||||
"rand_core 0.4.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc"
|
||||
|
||||
[[package]]
|
||||
name = "rand_hc"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7b40677c7be09ae76218dc623efbf7b18e34bced3f38883af07bb75630a21bc4"
|
||||
dependencies = [
|
||||
"rand_core 0.3.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_isaac"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ded997c9d5f13925be2a6fd7e66bf1872597f759fd9dd93513dd7e92e5a5ee08"
|
||||
dependencies = [
|
||||
"rand_core 0.3.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_jitter"
|
||||
version = "0.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1166d5c91dc97b88d1decc3285bb0a99ed84b05cfd0bc2341bdf2d43fc41e39b"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"rand_core 0.4.2",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_os"
|
||||
version = "0.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7b75f676a1e053fc562eafbb47838d67c84801e38fc1ba459e8f180deabd5071"
|
||||
dependencies = [
|
||||
"cloudabi",
|
||||
"fuchsia-cprng",
|
||||
"libc",
|
||||
"rand_core 0.4.2",
|
||||
"rdrand",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_pcg"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "abf9b09b01790cfe0364f52bf32995ea3c39f4d2dd011eac241d2914146d0b44"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"rand_core 0.4.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_xorshift"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cbf7e9e623549b0e21f6e97cf8ecf247c1a8fd2e8a992ae265314300b2455d5c"
|
||||
dependencies = [
|
||||
"rand_core 0.3.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rdrand"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2"
|
||||
dependencies = [
|
||||
"rand_core 0.3.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||
dependencies = [
|
||||
"winapi-i686-pc-windows-gnu",
|
||||
"winapi-x86_64-pc-windows-gnu",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-i686-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
10
rustbook/guessing_game/Cargo.toml
Normal file
10
rustbook/guessing_game/Cargo.toml
Normal file
@@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "guessing_game"
|
||||
version = "0.1.0"
|
||||
authors = ["Dan Buch <dan@meatballhat.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rand = "0.6.0"
|
35
rustbook/guessing_game/src/main.rs
Normal file
35
rustbook/guessing_game/src/main.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use rand::Rng;
|
||||
use std::cmp::Ordering;
|
||||
use std::io;
|
||||
|
||||
fn main() {
|
||||
println!("Guess the number!");
|
||||
|
||||
let secret_number = rand::thread_rng().gen_range(1, 101);
|
||||
|
||||
loop {
|
||||
println!("Please input your guess.");
|
||||
|
||||
let mut guess = String::new();
|
||||
|
||||
io::stdin()
|
||||
.read_line(&mut guess)
|
||||
.expect("Failed to read line");
|
||||
|
||||
let guess: u32 = match guess.trim().parse() {
|
||||
Ok(num) => num,
|
||||
Err(_) => continue,
|
||||
};
|
||||
|
||||
println!("You guessed: {}", guess);
|
||||
|
||||
match guess.cmp(&secret_number) {
|
||||
Ordering::Less => println!("Too small!"),
|
||||
Ordering::Greater => println!("Too big!"),
|
||||
Ordering::Equal => {
|
||||
println!("You win!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
5
rustbook/hello_cargo/Cargo.lock
generated
Normal file
5
rustbook/hello_cargo/Cargo.lock
generated
Normal file
@@ -0,0 +1,5 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "hello_cargo"
|
||||
version = "0.1.0"
|
9
rustbook/hello_cargo/Cargo.toml
Normal file
9
rustbook/hello_cargo/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "hello_cargo"
|
||||
version = "0.1.0"
|
||||
authors = ["Dan Buch <dan@meatballhat.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
3
rustbook/hello_cargo/src/main.rs
Normal file
3
rustbook/hello_cargo/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
3
rustbook/hello_world/main.rs
Normal file
3
rustbook/hello_world/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
5
rustbook/httpong/Cargo.lock
generated
Normal file
5
rustbook/httpong/Cargo.lock
generated
Normal file
@@ -0,0 +1,5 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "httpong"
|
||||
version = "0.1.0"
|
9
rustbook/httpong/Cargo.toml
Normal file
9
rustbook/httpong/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "httpong"
|
||||
version = "0.1.0"
|
||||
authors = ["Dan Buch <dan@meatballhat.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
37
rustbook/httpong/src/main.rs
Normal file
37
rustbook/httpong/src/main.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use std::env;
|
||||
use std::io::Read;
|
||||
use std::net::{TcpListener, TcpStream};
|
||||
|
||||
fn handle_client(stream: &mut TcpStream) -> std::io::Result<()> {
|
||||
println!("---> handling stream {:?}", stream);
|
||||
let mut buf = [0; 1024];
|
||||
let n = stream.read(&mut buf[..])?;
|
||||
let req = std::str::from_utf8(&buf[..n]);
|
||||
match req {
|
||||
Ok(s) => {
|
||||
println!("---> first {} bytes: {:?}", n, s);
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => {
|
||||
println!("---> oh no {:?}", e);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
let mut addr = String::from("127.0.0.1:19721");
|
||||
if let Ok(v) = env::var("ADDR") {
|
||||
addr = String::from(v.as_str());
|
||||
}
|
||||
|
||||
println!("listening at {}", addr);
|
||||
let listener = TcpListener::bind(addr)?;
|
||||
|
||||
for stream in listener.incoming() {
|
||||
if let Ok(_) = handle_client(&mut stream?) {
|
||||
println!("---> looks like that went well")
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
5
rustbook/matchy/Cargo.lock
generated
Normal file
5
rustbook/matchy/Cargo.lock
generated
Normal file
@@ -0,0 +1,5 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "matchy"
|
||||
version = "0.1.0"
|
9
rustbook/matchy/Cargo.toml
Normal file
9
rustbook/matchy/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "matchy"
|
||||
version = "0.1.0"
|
||||
authors = ["Dan Buch <dan@meatballhat.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
22
rustbook/matchy/src/main.rs
Normal file
22
rustbook/matchy/src/main.rs
Normal file
@@ -0,0 +1,22 @@
|
||||
enum Coin {
|
||||
Penny,
|
||||
Nickel,
|
||||
Dime,
|
||||
Quarter,
|
||||
}
|
||||
|
||||
fn value_in_cents(coin: Coin) -> u8 {
|
||||
match coin {
|
||||
Coin::Penny => 1,
|
||||
Coin::Nickel => 5,
|
||||
Coin::Dime => 10,
|
||||
Coin::Quarter => 25,
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Penny is {:?}", value_in_cents(Coin::Penny));
|
||||
println!("Nickel is {:?}", value_in_cents(Coin::Nickel));
|
||||
println!("Dime is {:?}", value_in_cents(Coin::Dime));
|
||||
println!("Quarter is {:?}", value_in_cents(Coin::Quarter));
|
||||
}
|
5
rustbook/scarytxt/Cargo.lock
generated
Normal file
5
rustbook/scarytxt/Cargo.lock
generated
Normal file
@@ -0,0 +1,5 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "scarytxt"
|
||||
version = "0.1.0"
|
9
rustbook/scarytxt/Cargo.toml
Normal file
9
rustbook/scarytxt/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "scarytxt"
|
||||
version = "0.1.0"
|
||||
authors = ["Dan Buch <dan@meatballhat.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
229
rustbook/scarytxt/src/game.rs
Normal file
229
rustbook/scarytxt/src/game.rs
Normal file
@@ -0,0 +1,229 @@
|
||||
use std::io::{self, Write};
|
||||
use std::process;
|
||||
|
||||
pub struct Game {
|
||||
encounter: Option<Encounter>,
|
||||
zone: Zone,
|
||||
adventurer: Adventurer,
|
||||
scarers: Vec<Scarer>,
|
||||
}
|
||||
|
||||
impl Game {
|
||||
pub fn new() -> Game {
|
||||
Game {
|
||||
encounter: None,
|
||||
zone: Zone { width: 4, depth: 4 },
|
||||
adventurer: Adventurer {
|
||||
name: String::from("adv"),
|
||||
h: Humors {
|
||||
health: 1000,
|
||||
attack: 1,
|
||||
critical_pct: 10,
|
||||
},
|
||||
l: Location { x: 0, y: 0 },
|
||||
},
|
||||
scarers: vec![Scarer {
|
||||
name: String::from("Blue Minotaur"),
|
||||
description: String::from("Towering half bull with flaming nostrils"),
|
||||
h: Humors {
|
||||
health: 4000,
|
||||
attack: 4,
|
||||
critical_pct: 2,
|
||||
},
|
||||
l: Location { x: 2, y: 2 },
|
||||
}],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn play(&mut self) {
|
||||
loop {
|
||||
self.step();
|
||||
}
|
||||
}
|
||||
|
||||
fn on(&mut self, event: Event) {
|
||||
match event {
|
||||
Event::ApproachEdge => {
|
||||
println!("// you stand at the edge of the zone");
|
||||
}
|
||||
Event::Encounter(s) => {
|
||||
println!("// !! you encounter an {}", s.name);
|
||||
}
|
||||
Event::Unencounter(s, o) => {
|
||||
println!("// !! you unencountered an {} with outcome {:?}", s.name, o);
|
||||
}
|
||||
Event::Fall => {
|
||||
println!("// you fall out of the zone");
|
||||
process::exit(0);
|
||||
}
|
||||
Event::Step(d) => {
|
||||
println!("// you step {:?}", d);
|
||||
}
|
||||
Event::Miss => {
|
||||
println!("// you brace yourself for the next step");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn step(&mut self) {
|
||||
{
|
||||
let al = &self.adventurer.l;
|
||||
|
||||
if al.x == 0
|
||||
|| al.y == 0
|
||||
|| al.x as u8 == self.zone.width
|
||||
|| al.y as u8 == self.zone.depth
|
||||
{
|
||||
self.on(Event::ApproachEdge);
|
||||
}
|
||||
}
|
||||
|
||||
let adventurer_loc = Location {
|
||||
x: self.adventurer.l.x,
|
||||
y: self.adventurer.l.y,
|
||||
};
|
||||
|
||||
let evt: Event = Event::Miss;
|
||||
|
||||
for s in self.scarers.iter() {
|
||||
if adventurer_loc == s.l {
|
||||
evt = Event::Encounter(*s);
|
||||
}
|
||||
}
|
||||
|
||||
self.on(evt);
|
||||
|
||||
print!("> ");
|
||||
io::stdout().flush().expect("failed to flush stdout");
|
||||
|
||||
let mut line = String::new();
|
||||
io::stdin()
|
||||
.read_line(&mut line)
|
||||
.expect("failed to read line of input");
|
||||
line = line.trim().to_string();
|
||||
|
||||
match &mut self.encounter {
|
||||
None => { mut self.wandering_step(&line) },
|
||||
Some(e) => { mut self.encounter_step(&line, e) },
|
||||
}
|
||||
}
|
||||
|
||||
fn encounter_step(&self, line: &str, enc: &mut Encounter) {
|
||||
eprintln!(
|
||||
"// this is not an encounter {:?} from line {:?} OK",
|
||||
enc, line
|
||||
);
|
||||
}
|
||||
|
||||
fn wandering_step(&mut self, line: &str) {
|
||||
let v: Vec<&str> = line.split(' ').collect();
|
||||
match v.get(0) {
|
||||
Some(cmd) => {
|
||||
match *cmd {
|
||||
"help" => {
|
||||
println!("// maybe try 'loc'");
|
||||
}
|
||||
"loc" => {
|
||||
println!(
|
||||
"// you stand at ({}, {})",
|
||||
self.adventurer.l.x, self.adventurer.l.y
|
||||
);
|
||||
}
|
||||
"step" => {
|
||||
let (mv, dir) = match v.get(1) {
|
||||
Some(direction) => {
|
||||
let direction = direction.trim().to_lowercase();
|
||||
match direction.as_str() {
|
||||
"forward" => ((1i8, 0i8), Direction::Forward),
|
||||
"back" => ((-1i8, 0i8), Direction::Back),
|
||||
"left" => ((0i8, 1i8), Direction::Left),
|
||||
"right" => ((0i8, -1i8), Direction::Right),
|
||||
_ => {
|
||||
println!("unknown direction {}", direction);
|
||||
((0i8, 0i8), Direction::Forward)
|
||||
}
|
||||
}
|
||||
}
|
||||
None => ((1i8, 0i8), Direction::Forward),
|
||||
};
|
||||
|
||||
let loc = &mut self.adventurer.l;
|
||||
loc.x += mv.0;
|
||||
loc.y += mv.1;
|
||||
println!("// you step {:?}", dir);
|
||||
}
|
||||
_ => {
|
||||
println!("// what is this {}?", cmd);
|
||||
}
|
||||
};
|
||||
}
|
||||
None => println!("// ..."),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Direction {
|
||||
Forward,
|
||||
Back,
|
||||
Left,
|
||||
Right,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum EncounterOutcome {
|
||||
Victory,
|
||||
Defeat,
|
||||
}
|
||||
|
||||
enum Event {
|
||||
Fall,
|
||||
ApproachEdge,
|
||||
Step(Direction),
|
||||
Encounter(Scarer),
|
||||
Unencounter(Scarer, EncounterOutcome),
|
||||
Miss,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Encounter {
|
||||
scarer: Scarer,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Humors {
|
||||
health: u16,
|
||||
attack: u8,
|
||||
critical_pct: u8,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Location {
|
||||
x: i8,
|
||||
y: i8,
|
||||
}
|
||||
|
||||
impl PartialEq for Location {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.x == other.x && self.y == other.y
|
||||
}
|
||||
}
|
||||
|
||||
struct Adventurer {
|
||||
name: String,
|
||||
h: Humors,
|
||||
l: Location,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Scarer {
|
||||
name: String,
|
||||
description: String,
|
||||
h: Humors,
|
||||
l: Location,
|
||||
}
|
||||
|
||||
struct Zone {
|
||||
width: u8,
|
||||
depth: u8,
|
||||
}
|
5
rustbook/scarytxt/src/main.rs
Normal file
5
rustbook/scarytxt/src/main.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
pub mod game;
|
||||
|
||||
fn main() {
|
||||
game::Game::new().play();
|
||||
}
|
5
rustbook/structs/Cargo.lock
generated
Normal file
5
rustbook/structs/Cargo.lock
generated
Normal file
@@ -0,0 +1,5 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "structs"
|
||||
version = "0.1.0"
|
9
rustbook/structs/Cargo.toml
Normal file
9
rustbook/structs/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "structs"
|
||||
version = "0.1.0"
|
||||
authors = ["Dan Buch <dan@meatballhat.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
38
rustbook/structs/src/main.rs
Normal file
38
rustbook/structs/src/main.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
#[derive(Debug)]
|
||||
struct Rectangle {
|
||||
width: u32,
|
||||
height: u32,
|
||||
}
|
||||
|
||||
impl Rectangle {
|
||||
fn area(&self) -> u32 {
|
||||
self.width * self.height
|
||||
}
|
||||
|
||||
fn can_hold(&self, other: &Rectangle) -> bool {
|
||||
self.width > other.width && self.height > other.height
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let rect1 = Rectangle {
|
||||
width: 30,
|
||||
height: 50,
|
||||
};
|
||||
let rect2 = Rectangle {
|
||||
width: 10,
|
||||
height: 40,
|
||||
};
|
||||
let rect3 = Rectangle {
|
||||
width: 60,
|
||||
height: 45,
|
||||
};
|
||||
|
||||
println!(
|
||||
"The area of the rectangle is {} square pixels.",
|
||||
rect1.area()
|
||||
);
|
||||
|
||||
println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2));
|
||||
println!("Can rect1 hold rect3? {}", rect1.can_hold(&rect3));
|
||||
}
|
5
rustbook/variables/Cargo.lock
generated
Normal file
5
rustbook/variables/Cargo.lock
generated
Normal file
@@ -0,0 +1,5 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "variables"
|
||||
version = "0.1.0"
|
9
rustbook/variables/Cargo.toml
Normal file
9
rustbook/variables/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "variables"
|
||||
version = "0.1.0"
|
||||
authors = ["Dan Buch <dan@meatballhat.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
103
rustbook/variables/src/main.rs
Normal file
103
rustbook/variables/src/main.rs
Normal file
@@ -0,0 +1,103 @@
|
||||
use std::env;
|
||||
use std::process;
|
||||
|
||||
fn main() {
|
||||
let mut command = String::new();
|
||||
let mut command_args = Vec::<String>::new();
|
||||
for (i, arg) in env::args().enumerate() {
|
||||
match i {
|
||||
0 => continue,
|
||||
1 => {
|
||||
command = arg;
|
||||
continue;
|
||||
}
|
||||
_ => command_args.push(arg.clone()),
|
||||
}
|
||||
}
|
||||
|
||||
match command.as_str() {
|
||||
"tc" => {
|
||||
if command_args.len() < 1 {
|
||||
eprintln!("ERROR: missing a value to convert");
|
||||
process::exit(1);
|
||||
}
|
||||
temp_convert(&command_args[0])
|
||||
}
|
||||
"fib" => {
|
||||
if command_args.len() < 1 {
|
||||
eprintln!("ERROR: missing a value to fib");
|
||||
process::exit(1);
|
||||
}
|
||||
let n: u64 = command_args[0]
|
||||
.parse()
|
||||
.expect("could not parse value to fib");
|
||||
if n >= 40 {
|
||||
eprintln!("WARN: this might take a while");
|
||||
}
|
||||
fib_n(n)
|
||||
}
|
||||
"rx" => redefine_x(),
|
||||
"help" => show_help(),
|
||||
_ => {
|
||||
show_help();
|
||||
eprintln!("ERROR: missing sub-command");
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn show_help() {
|
||||
println!("Usage: variables <command> [command_args]");
|
||||
println!();
|
||||
println!("The value for <command> may be one of:");
|
||||
println!(" fib - generate Nth Fibonacci number");
|
||||
println!(" tc - convert temperatures between Fahrenheit and Celsius");
|
||||
println!(" rx - redefine x");
|
||||
println!();
|
||||
}
|
||||
|
||||
fn redefine_x() {
|
||||
let mut x = 5;
|
||||
println!("The value of x is: {}", x);
|
||||
x = 6;
|
||||
println!("The value of x is: {}", x);
|
||||
}
|
||||
|
||||
fn temp_convert(to_convert: &str) {
|
||||
let to_convert = to_convert.to_lowercase();
|
||||
if to_convert.ends_with("c") {
|
||||
let c: f32 = to_convert
|
||||
.trim_end_matches('c')
|
||||
.parse()
|
||||
.expect("failed to parse temperature");
|
||||
println!("{}F", temp_convert_c2f(c));
|
||||
} else if to_convert.ends_with("f") {
|
||||
let f: f32 = to_convert
|
||||
.trim_end_matches('f')
|
||||
.parse()
|
||||
.expect("failed to parse temperature");
|
||||
println!("{}C", temp_convert_f2c(f));
|
||||
} else {
|
||||
eprintln!("ERROR: What are we supposed to do to {:?}?", to_convert);
|
||||
}
|
||||
}
|
||||
|
||||
fn temp_convert_c2f(c: f32) -> f32 {
|
||||
(c * (9.0 / 5.0)) + 32.0
|
||||
}
|
||||
|
||||
fn temp_convert_f2c(f: f32) -> f32 {
|
||||
(f - 32.0) * (5.0 / 9.0)
|
||||
}
|
||||
|
||||
fn fib_n(n: u64) {
|
||||
println!("{}", fib_up_to(n));
|
||||
}
|
||||
|
||||
fn fib_up_to(n: u64) -> u64 {
|
||||
match n {
|
||||
0 => 0,
|
||||
1 => 1,
|
||||
_ => fib_up_to(n - 1) + fib_up_to(n - 2),
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user