Compare commits

..

No commits in common. "9e2ff7d037a3c72ccedb7818350e98baca445506" and "4d9c346a173bb722b929f3ea3c00f84954483e24" have entirely different histories.

14 changed files with 112 additions and 144 deletions

View file

@ -4,9 +4,8 @@ enum Message {
}
fn main() {
println!("{:?}", Message::Resize);
println!("{:?}", Message::Move);
println!("{:?}", Message::Echo);
println!("{:?}", Message::ChangeColor);
println!("{:?}", Message::Quit);
println!("{:?}", Message::Echo);
println!("{:?}", Message::Move);
println!("{:?}", Message::ChangeColor);
}

View file

@ -1,11 +1,4 @@
#![allow(dead_code)]
#[derive(Debug)]
struct Point {
x: u64,
y: u64,
}
#[allow(dead_code)]
#[derive(Debug)]
enum Message {
// TODO: Define the different variants used below.
@ -19,11 +12,7 @@ impl Message {
fn main() {
let messages = [
Message::Resize {
width: 10,
height: 30,
},
Message::Move(Point { x: 10, y: 15 }),
Message::Move { x: 10, y: 30 },
Message::Echo(String::from("hello world")),
Message::ChangeColor(200, 255, 255),
Message::Quit,

View file

@ -1,35 +1,20 @@
struct Point {
x: u64,
y: u64,
}
enum Message {
// TODO: Implement the message variant types based on their usage below.
}
struct Point {
x: u8,
y: u8,
}
struct State {
width: u64,
height: u64,
position: Point,
message: String,
color: (u8, u8, u8),
position: Point,
quit: bool,
message: String,
}
impl State {
fn resize(&mut self, width: u64, height: u64) {
self.width = width;
self.height = height;
}
fn move_position(&mut self, point: Point) {
self.position = point;
}
fn echo(&mut self, s: String) {
self.message = s;
}
fn change_color(&mut self, color: (u8, u8, u8)) {
self.color = color;
}
@ -38,6 +23,14 @@ impl State {
self.quit = true;
}
fn echo(&mut self, s: String) {
self.message = s;
}
fn move_position(&mut self, point: Point) {
self.position = point;
}
fn process(&mut self, message: Message) {
// TODO: Create a match expression to process the different message variants.
// Remember: When passing a tuple as a function argument, you'll need extra parentheses:
@ -56,29 +49,21 @@ mod tests {
#[test]
fn test_match_message_call() {
let mut state = State {
width: 0,
height: 0,
position: Point { x: 0, y: 0 },
message: String::from("hello world"),
color: (0, 0, 0),
quit: false,
position: Point { x: 0, y: 0 },
color: (0, 0, 0),
message: String::from("hello world"),
};
state.process(Message::Resize {
width: 10,
height: 30,
});
state.process(Message::Move(Point { x: 10, y: 15 }));
state.process(Message::Echo(String::from("Hello world!")));
state.process(Message::ChangeColor(255, 0, 255));
state.process(Message::Echo(String::from("Hello world!")));
state.process(Message::Move(Point { x: 10, y: 15 }));
state.process(Message::Quit);
assert_eq!(state.width, 10);
assert_eq!(state.height, 30);
assert_eq!(state.color, (255, 0, 255));
assert_eq!(state.position.x, 10);
assert_eq!(state.position.y, 15);
assert_eq!(state.message, "Hello world!");
assert_eq!(state.color, (255, 0, 255));
assert!(state.quit);
assert_eq!(state.message, "Hello world!");
}
}

View file

@ -5,8 +5,7 @@
// Apple (4), Mango (2) and Lychee (5) are already in the basket hash map. You
// must add fruit to the basket so that there is at least one of each kind and
// more than 11 in total - we have a lot of mouths to feed. You are not allowed
// to insert any more of the fruits that are already in the basket (Apple,
// Mango, and Lyche).
// to insert any more of these fruits!
use std::collections::HashMap;

View file

@ -5,12 +5,12 @@
//
// Mary is buying apples. The price of an apple is calculated as follows:
// - An apple costs 2 rustbucks.
// - However, if Mary buys more than 40 apples, the price of each apple in the
// entire order is reduced to only 1 rustbuck!
// - If Mary buys more than 40 apples, each apple only costs 1 rustbuck!
// TODO: Write a function that calculates the price of an order of apples given
// the quantity bought.
// fn calculate_price_of_apples(???) -> ??? { ??? }
// Put your function here!
// fn calculate_price_of_apples(???) -> ??? {
fn main() {
// You can optionally experiment here.

View file

@ -445,7 +445,7 @@ dir = "08_enums"
test = false
hint = """
You can create enumerations that have different variants with different types
such as anonymous structs, structs, a single string, tuples, no data, etc."""
such as no data, anonymous structs, a single string, tuples, etc."""
[[exercises]]
name = "enums3"

View file

@ -1,16 +1,14 @@
#[derive(Debug)]
enum Message {
Resize,
Move,
Echo,
ChangeColor,
Quit,
Echo,
Move,
ChangeColor,
}
fn main() {
println!("{:?}", Message::Resize);
println!("{:?}", Message::Move);
println!("{:?}", Message::Echo);
println!("{:?}", Message::ChangeColor);
println!("{:?}", Message::Quit);
println!("{:?}", Message::Echo);
println!("{:?}", Message::Move);
println!("{:?}", Message::ChangeColor);
}

View file

@ -1,15 +1,7 @@
#![allow(dead_code)]
#[derive(Debug)]
struct Point {
x: u64,
y: u64,
}
#[allow(dead_code)]
#[derive(Debug)]
enum Message {
Resize { width: u64, height: u64 },
Move(Point),
Move { x: i64, y: i64 },
Echo(String),
ChangeColor(u8, u8, u8),
Quit,
@ -23,11 +15,7 @@ impl Message {
fn main() {
let messages = [
Message::Resize {
width: 10,
height: 30,
},
Message::Move(Point { x: 10, y: 15 }),
Message::Move { x: 10, y: 30 },
Message::Echo(String::from("hello world")),
Message::ChangeColor(200, 255, 255),
Message::Quit,

View file

@ -1,39 +1,23 @@
struct Point {
x: u64,
y: u64,
}
enum Message {
Resize { width: u64, height: u64 },
Move(Point),
Echo(String),
ChangeColor(u8, u8, u8),
Echo(String),
Move(Point),
Quit,
}
struct Point {
x: u8,
y: u8,
}
struct State {
width: u64,
height: u64,
position: Point,
message: String,
color: (u8, u8, u8),
position: Point,
quit: bool,
message: String,
}
impl State {
fn resize(&mut self, width: u64, height: u64) {
self.width = width;
self.height = height;
}
fn move_position(&mut self, point: Point) {
self.position = point;
}
fn echo(&mut self, s: String) {
self.message = s;
}
fn change_color(&mut self, color: (u8, u8, u8)) {
self.color = color;
}
@ -42,12 +26,19 @@ impl State {
self.quit = true;
}
fn echo(&mut self, s: String) {
self.message = s;
}
fn move_position(&mut self, point: Point) {
self.position = point;
}
fn process(&mut self, message: Message) {
match message {
Message::Resize { width, height } => self.resize(width, height),
Message::Move(point) => self.move_position(point),
Message::Echo(s) => self.echo(s),
Message::ChangeColor(r, g, b) => self.change_color((r, g, b)),
Message::Echo(s) => self.echo(s),
Message::Move(point) => self.move_position(point),
Message::Quit => self.quit(),
}
}
@ -64,29 +55,21 @@ mod tests {
#[test]
fn test_match_message_call() {
let mut state = State {
width: 0,
height: 0,
position: Point { x: 0, y: 0 },
message: String::from("hello world"),
color: (0, 0, 0),
quit: false,
position: Point { x: 0, y: 0 },
color: (0, 0, 0),
message: String::from("hello world"),
};
state.process(Message::Resize {
width: 10,
height: 30,
});
state.process(Message::Move(Point { x: 10, y: 15 }));
state.process(Message::Echo(String::from("Hello world!")));
state.process(Message::ChangeColor(255, 0, 255));
state.process(Message::Echo(String::from("Hello world!")));
state.process(Message::Move(Point { x: 10, y: 15 }));
state.process(Message::Quit);
assert_eq!(state.width, 10);
assert_eq!(state.height, 30);
assert_eq!(state.color, (255, 0, 255));
assert_eq!(state.position.x, 10);
assert_eq!(state.position.y, 15);
assert_eq!(state.message, "Hello world!");
assert_eq!(state.color, (255, 0, 255));
assert!(state.quit);
assert_eq!(state.message, "Hello world!");
}
}

View file

@ -47,8 +47,8 @@ fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Pr
.sum()
}
// Equivalent to `count_collection_iterator` and `count_iterator`, iterating as
// if the collection was a single container instead of a container of containers
// Equivalent to `count_collection_iterator`+`count_iterator`, iterating as if
// the collection was a single container instead of a container of containers
// (and more accurately, a single iterator instead of an iterator of iterators).
fn count_collection_iterator_flat(
collection: &[HashMap<String, Progress>],
@ -71,9 +71,10 @@ fn main() {
#[cfg(test)]
mod tests {
use super::*;
use Progress::*;
fn get_map() -> HashMap<String, Progress> {
use Progress::*;
let mut map = HashMap::new();
map.insert(String::from("variables1"), Complete);
map.insert(String::from("functions1"), Complete);
@ -86,6 +87,8 @@ mod tests {
}
fn get_vec_map() -> Vec<HashMap<String, Progress>> {
use Progress::*;
let map = get_map();
let mut other = HashMap::new();
@ -101,25 +104,25 @@ mod tests {
#[test]
fn count_complete() {
let map = get_map();
assert_eq!(count_iterator(&map, Complete), 3);
assert_eq!(count_iterator(&map, Progress::Complete), 3);
}
#[test]
fn count_some() {
let map = get_map();
assert_eq!(count_iterator(&map, Some), 1);
assert_eq!(count_iterator(&map, Progress::Some), 1);
}
#[test]
fn count_none() {
let map = get_map();
assert_eq!(count_iterator(&map, None), 2);
assert_eq!(count_iterator(&map, Progress::None), 2);
}
#[test]
fn count_complete_equals_for() {
let map = get_map();
let progress_states = [Complete, Some, None];
let progress_states = [Progress::Complete, Progress::Some, Progress::None];
for progress_state in progress_states {
assert_eq!(
count_for(&map, progress_state),
@ -131,28 +134,40 @@ mod tests {
#[test]
fn count_collection_complete() {
let collection = get_vec_map();
assert_eq!(count_collection_iterator(&collection, Complete), 6);
assert_eq!(count_collection_iterator_flat(&collection, Complete), 6);
assert_eq!(
count_collection_iterator(&collection, Progress::Complete),
6,
);
assert_eq!(
count_collection_iterator_flat(&collection, Progress::Complete),
6,
);
}
#[test]
fn count_collection_some() {
let collection = get_vec_map();
assert_eq!(count_collection_iterator(&collection, Some), 1);
assert_eq!(count_collection_iterator_flat(&collection, Some), 1);
assert_eq!(count_collection_iterator(&collection, Progress::Some), 1);
assert_eq!(
count_collection_iterator_flat(&collection, Progress::Some),
1
);
}
#[test]
fn count_collection_none() {
let collection = get_vec_map();
assert_eq!(count_collection_iterator(&collection, None), 4);
assert_eq!(count_collection_iterator_flat(&collection, None), 4);
assert_eq!(count_collection_iterator(&collection, Progress::None), 4);
assert_eq!(
count_collection_iterator_flat(&collection, Progress::None),
4
);
}
#[test]
fn count_collection_equals_for() {
let collection = get_vec_map();
let progress_states = [Complete, Some, None];
let progress_states = [Progress::Complete, Progress::Some, Progress::None];
for progress_state in progress_states {
assert_eq!(

View file

@ -15,7 +15,7 @@ fn char_counter<T: AsRef<str>>(arg: T) -> usize {
// Squares a number using `as_mut()`.
fn num_sq<T: AsMut<u32>>(arg: &mut T) {
let arg = arg.as_mut();
*arg *= *arg;
*arg = *arg * *arg;
}
fn main() {

View file

@ -1,7 +1,8 @@
// Mary is buying apples. The price of an apple is calculated as follows:
// - An apple costs 2 rustbucks.
// - However, if Mary buys more than 40 apples, the price of each apple in the
// entire order is reduced to only 1 rustbuck!
// - If Mary buys more than 40 apples, each apple only costs 1 rustbuck!
// Write a function that calculates the price of an order of apples given the
// quantity bought.
fn calculate_price_of_apples(n_apples: u64) -> u64 {
if n_apples > 40 {

View file

@ -1,3 +1,10 @@
// This is a quiz for the following sections:
// - Strings
// - Vecs
// - Move semantics
// - Modules
// - Enums
//
// Let's build a little machine in the form of a function. As input, we're going
// to give a list of strings and commands. These commands determine what action
// is going to be applied to the string. It can either be:

View file

@ -1,3 +1,7 @@
// This quiz tests:
// - Generics
// - Traits
//
// An imaginary magical school has a new report card generation system written
// in Rust! Currently, the system only supports creating report cards where the
// student's grade is represented numerically (e.g. 1.0 -> 5.5). However, the