diff --git a/exercises/error_handling/errors1.rs b/exercises/error_handling/errors1.rs index 13d2724cb..dc3cd93c8 100644 --- a/exercises/error_handling/errors1.rs +++ b/exercises/error_handling/errors1.rs @@ -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 @@ -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 { +pub fn generate_nametag_text(name: String) -> Result { 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)) } } diff --git a/exercises/options/options1.rs b/exercises/options/options1.rs index e131b48b9..b8ce3636a 100644 --- a/exercises/options/options1.rs +++ b/exercises/options/options1.rs @@ -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 :( @@ -13,7 +21,13 @@ fn maybe_icecream(time_of_day: u16) -> Option { // 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)] @@ -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)); } } diff --git a/exercises/options/options2.rs b/exercises/options/options2.rs index 4d998e7d0..e40a48d14 100644 --- a/exercises/options/options2.rs +++ b/exercises/options/options2.rs @@ -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] @@ -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)); } } @@ -32,7 +40,7 @@ mod tests { // TODO: make this a while let statement - remember that vector.pop also // adds another layer of Option. You can stack `Option`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; } diff --git a/exercises/options/options3.rs b/exercises/options/options3.rs index 23c15eab8..3ee6318e9 100644 --- a/exercises/options/options3.rs +++ b/exercises/options/options3.rs @@ -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, @@ -14,8 +22,9 @@ fn main() { let y: Option = 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. } diff --git a/exercises/quiz2.rs b/exercises/quiz2.rs index 29925cafc..8cf3584c6 100644 --- a/exercises/quiz2.rs +++ b/exercises/quiz2.rs @@ -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: @@ -20,8 +30,6 @@ // // No hints this time! -// I AM NOT DONE - pub enum Command { Uppercase, Trim, @@ -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 { // TODO: Complete the output declaration! - let mut output: ??? = vec![]; + let mut output: Vec = 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 } @@ -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]