Compare commits

...

11 commits

Author SHA1 Message Date
Mo 9e2ff7d037
Merge pull request #2021 from matthewjnield/main
fix: Add clarification to instructions for hashmaps2.rs
2024-07-05 15:52:20 +02:00
mo8it 65834fc420 Improve quizes 2024-07-05 15:38:59 +02:00
mo8it f5a4965de7 Improve wording in quiz1 2024-07-05 15:38:33 +02:00
mo8it db5911eb73 Use *= 2024-07-05 15:31:39 +02:00
Matt Nield 2f4e63b443
fix: Add clarification to instructions for hashmaps2.rs
In the instructions for hashmaps2.rs, the last sentence of the include the phrase "these fruits", which refers to fruits that were mentioned two sentences prior.

Having a sentence in between the first sentence in which the fruits were described and a later sentence in which the phrase "these fruits" is used makes this confusing to read, since the phrase "these fruits" does not come immediately after the mention of the fruits that the phrase refers to.

This pull request expands the last sentence to explicitly refer to the fruits being mentioned, in order to add clarity about the requirement of the exercise.
2024-07-05 08:27:51 -04:00
mo8it 584164a6ff Adjust enums exercises 2024-07-05 14:11:03 +02:00
mo8it e6f6d26d13 Import enum variants in all tests 2024-07-05 13:45:14 +02:00
Mo 67d8d5848c
Merge pull request #1774 from matthri/fix-enum-variant-inconsistency
Make enum variants more consistent between exercises
2024-07-05 13:43:25 +02:00
mo8it 43d15f09f0 Readd "structs" 2024-07-05 13:41:04 +02:00
mo8it 7123c7ae3a Merge remote-tracking branch 'upstream/main' into fix-enum-variant-inconsistency 2024-07-05 13:39:50 +02:00
Matthias Richter 77b687d501 fix(enums) make enum variants more consistent
closes #1545
2023-11-14 08:19:40 +01:00
14 changed files with 137 additions and 105 deletions

View file

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

View file

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

View file

@ -1,34 +1,41 @@
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 {
color: (u8, u8, u8),
width: u64,
height: u64,
position: Point,
quit: bool,
message: String,
color: (u8, u8, u8),
quit: bool,
}
impl State {
fn change_color(&mut self, color: (u8, u8, u8)) {
self.color = color;
fn resize(&mut self, width: u64, height: u64) {
self.width = width;
self.height = height;
}
fn quit(&mut self) {
self.quit = true;
fn move_position(&mut self, point: Point) {
self.position = point;
}
fn echo(&mut self, s: String) {
self.message = s;
}
fn move_position(&mut self, point: Point) {
self.position = point;
fn change_color(&mut self, color: (u8, u8, u8)) {
self.color = color;
}
fn quit(&mut self) {
self.quit = true;
}
fn process(&mut self, message: Message) {
@ -49,21 +56,29 @@ mod tests {
#[test]
fn test_match_message_call() {
let mut state = State {
quit: false,
width: 0,
height: 0,
position: Point { x: 0, y: 0 },
color: (0, 0, 0),
message: String::from("hello world"),
color: (0, 0, 0),
quit: false,
};
state.process(Message::ChangeColor(255, 0, 255));
state.process(Message::Echo(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::Quit);
assert_eq!(state.color, (255, 0, 255));
assert_eq!(state.width, 10);
assert_eq!(state.height, 30);
assert_eq!(state.position.x, 10);
assert_eq!(state.position.y, 15);
assert!(state.quit);
assert_eq!(state.message, "Hello world!");
assert_eq!(state.color, (255, 0, 255));
assert!(state.quit);
}
}

View file

@ -5,7 +5,8 @@
// 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 these fruits!
// to insert any more of the fruits that are already in the basket (Apple,
// Mango, and Lyche).
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.
// - If Mary buys more than 40 apples, each apple only costs 1 rustbuck!
// - However, if Mary buys more than 40 apples, the price of each apple in the
// entire order is reduced to only 1 rustbuck!
// TODO: Write a function that calculates the price of an order of apples given
// the quantity bought.
// Put your function here!
// fn calculate_price_of_apples(???) -> ??? {
// 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 no data, anonymous structs, a single string, tuples, etc."""
such as anonymous structs, structs, a single string, tuples, no data, etc."""
[[exercises]]
name = "enums3"

View file

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

View file

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

View file

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

View file

@ -47,8 +47,8 @@ fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Pr
.sum()
}
// Equivalent to `count_collection_iterator`+`count_iterator`, iterating as if
// the collection was a single container instead of a container of containers
// Equivalent to `count_collection_iterator` and `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,10 +71,9 @@ 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);
@ -87,8 +86,6 @@ mod tests {
}
fn get_vec_map() -> Vec<HashMap<String, Progress>> {
use Progress::*;
let map = get_map();
let mut other = HashMap::new();
@ -104,25 +101,25 @@ mod tests {
#[test]
fn count_complete() {
let map = get_map();
assert_eq!(count_iterator(&map, Progress::Complete), 3);
assert_eq!(count_iterator(&map, Complete), 3);
}
#[test]
fn count_some() {
let map = get_map();
assert_eq!(count_iterator(&map, Progress::Some), 1);
assert_eq!(count_iterator(&map, Some), 1);
}
#[test]
fn count_none() {
let map = get_map();
assert_eq!(count_iterator(&map, Progress::None), 2);
assert_eq!(count_iterator(&map, None), 2);
}
#[test]
fn count_complete_equals_for() {
let map = get_map();
let progress_states = [Progress::Complete, Progress::Some, Progress::None];
let progress_states = [Complete, Some, None];
for progress_state in progress_states {
assert_eq!(
count_for(&map, progress_state),
@ -134,40 +131,28 @@ mod tests {
#[test]
fn count_collection_complete() {
let collection = get_vec_map();
assert_eq!(
count_collection_iterator(&collection, Progress::Complete),
6,
);
assert_eq!(
count_collection_iterator_flat(&collection, Progress::Complete),
6,
);
assert_eq!(count_collection_iterator(&collection, Complete), 6);
assert_eq!(count_collection_iterator_flat(&collection, Complete), 6);
}
#[test]
fn count_collection_some() {
let collection = get_vec_map();
assert_eq!(count_collection_iterator(&collection, Progress::Some), 1);
assert_eq!(
count_collection_iterator_flat(&collection, Progress::Some),
1
);
assert_eq!(count_collection_iterator(&collection, Some), 1);
assert_eq!(count_collection_iterator_flat(&collection, Some), 1);
}
#[test]
fn count_collection_none() {
let collection = get_vec_map();
assert_eq!(count_collection_iterator(&collection, Progress::None), 4);
assert_eq!(
count_collection_iterator_flat(&collection, Progress::None),
4
);
assert_eq!(count_collection_iterator(&collection, None), 4);
assert_eq!(count_collection_iterator_flat(&collection, None), 4);
}
#[test]
fn count_collection_equals_for() {
let collection = get_vec_map();
let progress_states = [Progress::Complete, Progress::Some, Progress::None];
let progress_states = [Complete, Some, 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,8 +1,7 @@
// Mary is buying apples. The price of an apple is calculated as follows:
// - An apple costs 2 rustbucks.
// - 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.
// - However, if Mary buys more than 40 apples, the price of each apple in the
// entire order is reduced to only 1 rustbuck!
fn calculate_price_of_apples(n_apples: u64) -> u64 {
if n_apples > 40 {

View file

@ -1,10 +1,3 @@
// 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,7 +1,3 @@
// 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