mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-01-14 00:00:02 +03:00
Compare commits
11 commits
4d9c346a17
...
9e2ff7d037
Author | SHA1 | Date | |
---|---|---|---|
9e2ff7d037 | |||
65834fc420 | |||
f5a4965de7 | |||
db5911eb73 | |||
2f4e63b443 | |||
584164a6ff | |||
e6f6d26d13 | |||
67d8d5848c | |||
43d15f09f0 | |||
7123c7ae3a | |||
77b687d501 |
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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!(
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue