Merge pull request from 0xchasingfire/exercises-d2

enum and modules
This commit is contained in:
0xChasingFire 2021-05-21 17:18:44 +08:00 committed by GitHub
commit f9e8bf8e24
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 29 additions and 18 deletions

View file

@ -1,11 +1,13 @@
// enums1.rs
// Make me compile! Execute `rustlings hint enums1` for hints!
// I AM NOT DONE
#[derive(Debug)]
enum Message {
// TODO: define a few types of messages as used below
Quit,
Echo,
Move,
ChangeColor,
}
fn main() {

View file

@ -1,11 +1,13 @@
// enums2.rs
// Make me compile! Execute `rustlings hint enums2` for hints!
// I AM NOT DONE
#[derive(Debug)]
enum Message {
// TODO: define the different variants used below
Move{x: i32, y: i32},
Echo(String),
ChangeColor(i32, i32, i32),
Quit,
}
impl Message {

View file

@ -1,10 +1,12 @@
// enums3.rs
// Address all the TODOs to make the tests pass!
// I AM NOT DONE
enum Message {
// TODO: implement the message variant types based on their usage below
Move(Point),
Echo(String),
ChangeColor((u8, u8, u8)),
Quit,
}
struct Point {
@ -37,6 +39,12 @@ impl State {
fn process(&mut self, message: Message) {
// TODO: create a match expression to process the different message variants
match message {
Message::ChangeColor(color) => self.change_color(color),
Message::Move(point) => self.move_position(point),
Message::Echo(string) => self.echo(string),
Message::Quit => self.quit(),
}
}
}

View file

@ -1,10 +1,9 @@
// modules1.rs
// Make me compile! Execute `rustlings hint modules1` for hints :)
// I AM NOT DONE
mod sausage_factory {
fn make_sausage() {
pub mod sausage_factory {
pub fn make_sausage() {
println!("sausage!");
}
}

View file

@ -1,11 +1,9 @@
// modules2.rs
// Make me compile! Execute `rustlings hint modules2` for hints :)
// I AM NOT DONE
mod delicious_snacks {
use self::fruits::PEAR as fruit;
use self::veggies::CUCUMBER as veggie;
pub mod delicious_snacks {
pub use self::fruits::PEAR as fruit;
pub use self::veggies::CUCUMBER as veggie;
mod fruits {
pub const PEAR: &'static str = "Pear";

View file

@ -2,13 +2,15 @@
// Make me compile without adding any newlines or removing any of the lines.
// Execute `rustlings hint move_semantics5` for hints :)
// I AM NOT DONE
fn main() {
let mut x = 100;
let y = &mut x;
let z = &mut *y;
*y += 100;
{
let y = &mut x;
*y += 100;
}
let z = &mut x;
// *y += 100;
*z += 1000;
assert_eq!(x, 1200);
}