Skip to content

Commit

Permalink
2
Browse files Browse the repository at this point in the history
  • Loading branch information
7qing committed Jan 11, 2025
1 parent 3a0aca5 commit a9875a3
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 22 deletions.
18 changes: 13 additions & 5 deletions exercises/error_handling/errors1.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/*
* @Author: git.exe config user.name && error: git config user.email & please set dead value or install git
* @Date: 2025-01-11 11:25:38
* @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
* @LastEditTime: 2025-01-11 19:05:44
* @FilePath: /rust-rustlings-2024-autumn-7qing/exercises/error_handling/errors1.rs
* @Description:
*
* Copyright (c) 2025 by ${git_name_email}, All Rights Reserved.
*/
// errors1.rs
//
// This function refuses to generate text to be printed on a nametag if you pass
Expand All @@ -9,14 +19,12 @@
// Execute `rustlings hint errors1` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

pub fn generate_nametag_text(name: String) -> Option<String> {
pub fn generate_nametag_text(name: String) -> Result<String, String> {
if name.is_empty() {
// Empty names aren't allowed.
None
Err("`name` was empty; it must be nonempty.".into())
} else {
Some(format!("Hi! My name is {}", name))
Ok(format!("Hi! My name is {}", name))
}
}

Expand Down
22 changes: 18 additions & 4 deletions exercises/options/options1.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
/*
* @Author: git.exe config user.name && error: git config user.email & please set dead value or install git
* @Date: 2025-01-11 11:25:38
* @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
* @LastEditTime: 2025-01-11 15:44:42
* @FilePath: /rust-rustlings-2024-autumn-7qing/exercises/options/options1.rs
* @Description:
*
* Copyright (c) 2025 by ${git_name_email}, All Rights Reserved.
*/
// 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 :(
Expand All @@ -13,7 +21,13 @@ fn maybe_icecream(time_of_day: u16) -> Option<u16> {
// 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!
???
if time_of_day < 22 {
Some(5)
} else if time_of_day < 24 {
Some(0)
} else {
None
}
}

#[cfg(test)]
Expand All @@ -34,6 +48,6 @@ mod tests {
// 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, Some(5));
}
}
18 changes: 13 additions & 5 deletions exercises/options/options2.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
/*
* @Author: git.exe config user.name && error: git config user.email & please set dead value or install git
* @Date: 2025-01-11 11:25:38
* @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
* @LastEditTime: 2025-01-11 18:52:11
* @FilePath: /rust-rustlings-2024-autumn-7qing/exercises/options/options2.rs
* @Description:
*
* Copyright (c) 2025 by ${git_name_email}, All Rights Reserved.
*/
// options2.rs
//
// Execute `rustlings hint options2` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

#[cfg(test)]
mod tests {
#[test]
Expand All @@ -13,8 +21,8 @@ mod tests {
let optional_target = Some(target);

// TODO: Make this an if let statement whose value is "Some" type
word = optional_target {
assert_eq!(word, target);
if let word = optional_target {
assert_eq!(word, Some(target));
}
}

Expand All @@ -32,7 +40,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, cursor);
cursor -= 1;
}
Expand Down
15 changes: 12 additions & 3 deletions exercises/options/options3.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
/*
* @Author: git.exe config user.name && error: git config user.email & please set dead value or install git
* @Date: 2025-01-11 11:25:38
* @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
* @LastEditTime: 2025-01-11 18:56:29
* @FilePath: /rust-rustlings-2024-autumn-7qing/exercises/options/options3.rs
* @Description:
*
* Copyright (c) 2025 by ${git_name_email}, All Rights Reserved.
*/
// 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,
Expand All @@ -14,8 +22,9 @@ fn main() {
let y: Option<Point> = Some(Point { x: 100, y: 200 });

match y {
Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y),
Some(ref p) => println!("Co-ordinates are {},{} ", p.x, p.y),
_ => panic!("no match!"),
}

y; // Fix without deleting this line.
}
27 changes: 22 additions & 5 deletions exercises/quiz2.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/*
* @Author: git.exe config user.name && error: git config user.email & please set dead value or install git
* @Date: 2025-01-11 11:25:38
* @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
* @LastEditTime: 2025-01-11 15:37:19
* @FilePath: /rust-rustlings-2024-autumn-7qing/exercises/quiz2.rs
* @Description:
*
* Copyright (c) 2025 by ${git_name_email}, All Rights Reserved.
*/
// quiz2.rs
//
// This is a quiz for the following sections:
Expand All @@ -20,8 +30,6 @@
//
// No hints this time!

// I AM NOT DONE

pub enum Command {
Uppercase,
Trim,
Expand All @@ -32,11 +40,20 @@ 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::Trim => output.push(string.trim().to_string()),
Command::Uppercase => output.push(string.to_uppercase()),
Command::Append(x) => {
let mut result = string.clone();
result.push_str(&"bar".repeat(*x));
output.push(result);
}
}
}
output
}
Expand All @@ -45,7 +62,7 @@ mod my_module {
#[cfg(test)]
mod tests {
// TODO: What do we need to import to have `transformer` in scope?
use ???;
use super::my_module::transformer;
use super::Command;

#[test]
Expand Down

0 comments on commit a9875a3

Please sign in to comment.