mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-11 13:19:18 +00:00
saving more progress
This commit is contained in:
parent
83bb2a63d7
commit
416365a014
@ -13,5 +13,6 @@ return results to new Vec<i32> vector
|
||||
12) generics2. review https://doc.rust-lang.org/book/ch10-01-syntax.html
|
||||
13) traits1 why is self accepted since it's being updated?
|
||||
14) revisit all traits exercises
|
||||
|
||||
15) options1 (understand Some and None)
|
||||
16) option2 (important to understand if let, while let statements alternative to match ). ChatGPT explanation was great.
|
||||
|
||||
|
||||
@ -1,8 +1,6 @@
|
||||
// options1.rs
|
||||
// Execute `rustlings hint options1` or use the `hint` watch subcommand for a hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
// This function returns how much icecream there is left in the fridge.
|
||||
// If it's before 10PM, there's 5 pieces left. At 10PM, someone eats them
|
||||
// all, so there'll be no more left :(
|
||||
@ -10,7 +8,13 @@ fn maybe_icecream(time_of_day: u16) -> Option<u16> {
|
||||
// We use the 24-hour system here, so 10PM is a value of 22 and 12AM is a value of 0
|
||||
// The Option output should gracefully handle cases where time_of_day > 23.
|
||||
// TODO: Complete the function body - remember to return an Option!
|
||||
???
|
||||
|
||||
match time_of_day {
|
||||
n if n < 22 => Some(5),
|
||||
n if n >= 22 && n < 24 => Some(0),
|
||||
n => None
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@ -30,6 +34,6 @@ mod tests {
|
||||
fn raw_value() {
|
||||
// TODO: Fix this test. How do you get at the value contained in the Option?
|
||||
let icecreams = maybe_icecream(12);
|
||||
assert_eq!(icecreams, 5);
|
||||
assert_eq!(icecreams.unwrap(), 5);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,8 +1,6 @@
|
||||
// options2.rs
|
||||
// Execute `rustlings hint options2` or use the `hint` watch subcommand for a hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@ -13,7 +11,7 @@ mod tests {
|
||||
let optional_target = Some(target);
|
||||
|
||||
// TODO: Make this an if let statement whose value is "Some" type
|
||||
word = optional_target {
|
||||
if let Some(word) = optional_target {
|
||||
assert_eq!(word, target);
|
||||
}
|
||||
}
|
||||
@ -28,7 +26,7 @@ mod tests {
|
||||
|
||||
// TODO: make this a while let statement - remember that vector.pop also adds another layer of Option<T>
|
||||
// You can stack `Option<T>`'s into while let and if let
|
||||
integer = optional_integers.pop() {
|
||||
while let Some(Some(integer)) = optional_integers.pop() {
|
||||
assert_eq!(integer, range);
|
||||
range -= 1;
|
||||
}
|
||||
|
||||
@ -1,8 +1,6 @@
|
||||
// options3.rs
|
||||
// Execute `rustlings hint options3` or use the `hint` watch subcommand for a hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
struct Point {
|
||||
x: i32,
|
||||
y: i32,
|
||||
@ -11,7 +9,7 @@ struct Point {
|
||||
fn main() {
|
||||
let y: Option<Point> = Some(Point { x: 100, y: 200 });
|
||||
|
||||
match y {
|
||||
match &y {
|
||||
Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y),
|
||||
_ => println!("no match"),
|
||||
}
|
||||
|
||||
@ -18,8 +18,6 @@
|
||||
// - The output element is going to be a Vector of strings.
|
||||
// No hints this time!
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
pub enum Command {
|
||||
Uppercase,
|
||||
Trim,
|
||||
@ -30,11 +28,16 @@ mod my_module {
|
||||
use super::Command;
|
||||
|
||||
// TODO: Complete the function signature!
|
||||
pub fn transformer(input: ???) -> ??? {
|
||||
pub fn transformer(input: Vec<(String, Command)>) -> Vec<String> {
|
||||
// TODO: Complete the output declaration!
|
||||
let mut output: ??? = vec![];
|
||||
let mut output: Vec<String> = vec![];
|
||||
for (string, command) in input.iter() {
|
||||
// TODO: Complete the function body. You can do it!
|
||||
match command {
|
||||
Command::Uppercase => output.push(string.to_uppercase()),
|
||||
Command::Trim => output.push(string.trim().to_string()),
|
||||
Command::Append(n) => output.push(format!("{}{}",string, "bar".repeat(*n)))
|
||||
}
|
||||
}
|
||||
output
|
||||
}
|
||||
@ -43,7 +46,7 @@ mod my_module {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
// TODO: What do we need to import to have `transformer` in scope?
|
||||
use ???;
|
||||
use my_module::transformer as transformer;
|
||||
use super::Command;
|
||||
|
||||
#[test]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user