From 024fc6848605e6ea4661a081794e8f823b60473e Mon Sep 17 00:00:00 2001 From: Mina Date: Tue, 21 Jan 2025 03:26:36 -0300 Subject: [PATCH] update --- exercises/generics/generics1.rs | 4 +-- exercises/generics/generics2.rs | 10 ++++---- exercises/hashmaps/hashmaps1.rs | 8 ++++-- exercises/hashmaps/hashmaps2.rs | 5 +++- exercises/hashmaps/hashmaps3.rs | 27 +++++++++++++++++++- exercises/iterators/iterators1.rs | 10 ++++---- exercises/iterators/iterators2.rs | 28 +++++++++++++++++---- exercises/lifetimes/lifetimes1.rs | 4 +-- exercises/lifetimes/lifetimes2.rs | 12 +++++---- exercises/lifetimes/lifetimes3.rs | 8 +++--- exercises/macros/macros1.rs | 4 +-- exercises/macros/macros2.rs | 10 +++++--- exercises/modules/modules2.rs | 6 ++--- exercises/move_semantics/move_semantics4.rs | 8 +++--- exercises/move_semantics/move_semantics5.rs | 4 +-- exercises/move_semantics/move_semantics6.rs | 12 ++++----- exercises/options/options1.rs | 12 ++++++--- exercises/quiz2.rs | 23 +++++++++++++---- exercises/quiz3.rs | 11 ++++---- exercises/smart_pointers/arc1.rs | 6 ++--- exercises/smart_pointers/box1.rs | 10 +++++--- exercises/smart_pointers/cow1.rs | 8 +++++- exercises/smart_pointers/rc1.rs | 11 +++++--- exercises/strings/strings3.rs | 10 +++++--- exercises/strings/strings4.rs | 22 ++++++++-------- exercises/structs/structs1.rs | 13 ++++++---- exercises/traits/traits3.rs | 6 +++-- exercises/traits/traits4.rs | 4 +-- exercises/vecs/vecs1.rs | 3 +-- exercises/vecs/vecs2.rs | 7 +++--- 30 files changed, 199 insertions(+), 107 deletions(-) diff --git a/exercises/generics/generics1.rs b/exercises/generics/generics1.rs index 35c1d2f..2321f73 100644 --- a/exercises/generics/generics1.rs +++ b/exercises/generics/generics1.rs @@ -6,9 +6,9 @@ // Execute `rustlings hint generics1` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE + fn main() { - let mut shopping_list: Vec = Vec::new(); + let mut shopping_list: Vec<&str> = Vec::new(); shopping_list.push("milk"); } diff --git a/exercises/generics/generics2.rs b/exercises/generics/generics2.rs index 074cd93..ae12d35 100644 --- a/exercises/generics/generics2.rs +++ b/exercises/generics/generics2.rs @@ -6,14 +6,14 @@ // Execute `rustlings hint generics2` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE -struct Wrapper { - value: u32, + +struct Wrapper { + value: T, } -impl Wrapper { - pub fn new(value: u32) -> Self { +impl Wrapper { + pub fn new(value: T) -> Self { Wrapper { value } } } diff --git a/exercises/hashmaps/hashmaps1.rs b/exercises/hashmaps/hashmaps1.rs index 80829ea..fb433d3 100644 --- a/exercises/hashmaps/hashmaps1.rs +++ b/exercises/hashmaps/hashmaps1.rs @@ -11,17 +11,21 @@ // Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE + use std::collections::HashMap; fn fruit_basket() -> HashMap { - let mut basket = // TODO: declare your hash map here. + let mut basket = HashMap::new(); + // TODO: declare your hash map here. // Two bananas are already given for you :) basket.insert(String::from("banana"), 2); // TODO: Put more fruits in your basket here. + basket.insert(String::from("apple"),3); + + basket.insert(String::from("mango"),4); basket } diff --git a/exercises/hashmaps/hashmaps2.rs b/exercises/hashmaps/hashmaps2.rs index a592569..f1c6118 100644 --- a/exercises/hashmaps/hashmaps2.rs +++ b/exercises/hashmaps/hashmaps2.rs @@ -14,7 +14,7 @@ // Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE + use std::collections::HashMap; @@ -40,6 +40,9 @@ fn fruit_basket(basket: &mut HashMap) { // TODO: Insert new fruits if they are not already present in the // basket. Note that you are not allowed to put any type of fruit that's // already present! + if !basket.contains_key(&fruit){ + basket.insert(fruit,1); + } } } diff --git a/exercises/hashmaps/hashmaps3.rs b/exercises/hashmaps/hashmaps3.rs index 08e977c..2f63274 100644 --- a/exercises/hashmaps/hashmaps3.rs +++ b/exercises/hashmaps/hashmaps3.rs @@ -14,7 +14,7 @@ // Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE + use std::collections::HashMap; @@ -39,6 +39,31 @@ fn build_scores_table(results: String) -> HashMap { // will be the number of goals conceded from team_2, and similarly // goals scored by team_2 will be the number of goals conceded by // team_1. + let team_1_goals=Team{ + goals_scored:team_1_score, + goals_conceded:team_2_score, + }; + + let team_2_goals=Team{ + goals_scored:team_2_score, + goals_conceded:team_1_score, + }; + + if scores.contains_key(&team_1_name){ + let team_1= scores.get_mut(&team_1_name).unwrap(); + team_1.goals_scored=team_1.goals_scored+team_1_goals.goals_scored; + team_1.goals_conceded=team_1.goals_conceded+team_1_goals.goals_conceded; + }else{ + scores.insert(team_1_name,team_1_goals); + } + + if scores.contains_key(&team_2_name){ + let team_2= scores.get_mut(&team_2_name).unwrap(); + team_2.goals_scored=team_2.goals_scored+team_2_goals.goals_scored; + team_2.goals_conceded=team_2.goals_conceded+team_2_goals.goals_conceded; + }else{ + scores.insert(team_2_name,team_2_goals); + } } scores } diff --git a/exercises/iterators/iterators1.rs b/exercises/iterators/iterators1.rs index b3f698b..8aa65fe 100644 --- a/exercises/iterators/iterators1.rs +++ b/exercises/iterators/iterators1.rs @@ -9,17 +9,17 @@ // Execute `rustlings hint iterators1` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE + fn main() { let my_fav_fruits = vec!["banana", "custard apple", "avocado", "peach", "raspberry"]; - let mut my_iterable_fav_fruits = ???; // TODO: Step 1 + let mut my_iterable_fav_fruits = my_fav_fruits.iter(); // TODO: Step 1 assert_eq!(my_iterable_fav_fruits.next(), Some(&"banana")); - assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 2 + assert_eq!(my_iterable_fav_fruits.next(), Some(&"custard apple")); // TODO: Step 2 assert_eq!(my_iterable_fav_fruits.next(), Some(&"avocado")); - assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 3 + assert_eq!(my_iterable_fav_fruits.next(), Some(&"peach")); // TODO: Step 3 assert_eq!(my_iterable_fav_fruits.next(), Some(&"raspberry")); - assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 4 + assert_eq!(my_iterable_fav_fruits.next(), None); // TODO: Step 4 } diff --git a/exercises/iterators/iterators2.rs b/exercises/iterators/iterators2.rs index dda82a0..2243219 100644 --- a/exercises/iterators/iterators2.rs +++ b/exercises/iterators/iterators2.rs @@ -6,7 +6,7 @@ // Execute `rustlings hint iterators2` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE + // Step 1. // Complete the `capitalize_first` function. @@ -15,8 +15,18 @@ pub fn capitalize_first(input: &str) -> String { let mut c = input.chars(); match c.next() { None => String::new(), - Some(first) => ???, - } + Some(first) => { + let mut new_string=String::new(); + new_string.push(first.to_uppercase().next().unwrap()); + loop { + match c.next() { + Some(val) => new_string.push(val), + None => break, + } + } + new_string + } + } } // Step 2. @@ -24,7 +34,11 @@ pub fn capitalize_first(input: &str) -> String { // Return a vector of strings. // ["hello", "world"] -> ["Hello", "World"] pub fn capitalize_words_vector(words: &[&str]) -> Vec { - vec![] + let mut new_words=Vec::new(); + for i in 0..words.len(){ + new_words.push(capitalize_first(words[i])) + } + new_words } // Step 3. @@ -32,7 +46,11 @@ pub fn capitalize_words_vector(words: &[&str]) -> Vec { // Return a single string. // ["hello", " ", "world"] -> "Hello World" pub fn capitalize_words_string(words: &[&str]) -> String { - String::new() + let mut new_string=String::new(); + for i in 0..words.len(){ + new_string.push_str(&capitalize_first(words[i])) + } + new_string } #[cfg(test)] diff --git a/exercises/lifetimes/lifetimes1.rs b/exercises/lifetimes/lifetimes1.rs index 87bde49..b40fc1c 100644 --- a/exercises/lifetimes/lifetimes1.rs +++ b/exercises/lifetimes/lifetimes1.rs @@ -8,9 +8,9 @@ // Execute `rustlings hint lifetimes1` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE -fn longest(x: &str, y: &str) -> &str { + +fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { if x.len() > y.len() { x } else { diff --git a/exercises/lifetimes/lifetimes2.rs b/exercises/lifetimes/lifetimes2.rs index 4f3d8c1..b579672 100644 --- a/exercises/lifetimes/lifetimes2.rs +++ b/exercises/lifetimes/lifetimes2.rs @@ -6,7 +6,7 @@ // Execute `rustlings hint lifetimes2` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE + fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { if x.len() > y.len() { @@ -19,9 +19,11 @@ fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { fn main() { let string1 = String::from("long string is long"); let result; - { - let string2 = String::from("xyz"); - result = longest(string1.as_str(), string2.as_str()); - } + + let string2 = String::from("xyz"); + result = longest(string1.as_str(), string2.as_str()); + println!("The longest string is '{}'", result); + + } diff --git a/exercises/lifetimes/lifetimes3.rs b/exercises/lifetimes/lifetimes3.rs index 9c59f9c..7803e6c 100644 --- a/exercises/lifetimes/lifetimes3.rs +++ b/exercises/lifetimes/lifetimes3.rs @@ -5,11 +5,11 @@ // Execute `rustlings hint lifetimes3` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE -struct Book { - author: &str, - title: &str, + +struct Book<'a> { + author: &'a str, + title: &'a str, } fn main() { diff --git a/exercises/macros/macros1.rs b/exercises/macros/macros1.rs index 678de6e..eb0cbba 100644 --- a/exercises/macros/macros1.rs +++ b/exercises/macros/macros1.rs @@ -3,7 +3,7 @@ // Execute `rustlings hint macros1` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE + macro_rules! my_macro { () => { @@ -12,5 +12,5 @@ macro_rules! my_macro { } fn main() { - my_macro(); + my_macro!(); } diff --git a/exercises/macros/macros2.rs b/exercises/macros/macros2.rs index 788fc16..0df86ce 100644 --- a/exercises/macros/macros2.rs +++ b/exercises/macros/macros2.rs @@ -3,14 +3,16 @@ // Execute `rustlings hint macros2` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE -fn main() { - my_macro!(); -} + + macro_rules! my_macro { () => { println!("Check out my macro!"); }; } + +fn main() { + my_macro!(); +} \ No newline at end of file diff --git a/exercises/modules/modules2.rs b/exercises/modules/modules2.rs index 0415454..a16b8c9 100644 --- a/exercises/modules/modules2.rs +++ b/exercises/modules/modules2.rs @@ -7,12 +7,12 @@ // Execute `rustlings hint modules2` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE + mod delicious_snacks { // TODO: Fix these use statements - use self::fruits::PEAR as ??? - use self::veggies::CUCUMBER as ??? + pub use self::fruits::PEAR as fruit; + pub use self::veggies::CUCUMBER as veggie; mod fruits { pub const PEAR: &'static str = "Pear"; diff --git a/exercises/move_semantics/move_semantics4.rs b/exercises/move_semantics/move_semantics4.rs index eb3aef0..4dde38c 100644 --- a/exercises/move_semantics/move_semantics4.rs +++ b/exercises/move_semantics/move_semantics4.rs @@ -7,12 +7,12 @@ // Execute `rustlings hint move_semantics4` or use the `hint` watch subcommand // for a hint. -// I AM NOT DONE + fn main() { - let vec0 = Vec::new(); + //let vec0 = Vec::new(); - let mut vec1 = fill_vec(vec0); + let mut vec1 = fill_vec(); println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); @@ -23,7 +23,7 @@ fn main() { // `fill_vec()` no longer takes `vec: Vec` as argument fn fill_vec() -> Vec { - let mut vec = vec; + let mut vec:Vec = Vec::new(); vec.push(22); vec.push(44); diff --git a/exercises/move_semantics/move_semantics5.rs b/exercises/move_semantics/move_semantics5.rs index dc67c2f..60fe3ac 100644 --- a/exercises/move_semantics/move_semantics5.rs +++ b/exercises/move_semantics/move_semantics5.rs @@ -6,13 +6,13 @@ // Execute `rustlings hint move_semantics5` or use the `hint` watch subcommand // for a hint. -// I AM NOT DONE + fn main() { let mut x = 100; let y = &mut x; - let z = &mut x; *y += 100; + let z = &mut x; *z += 1000; assert_eq!(x, 1200); } \ No newline at end of file diff --git a/exercises/move_semantics/move_semantics6.rs b/exercises/move_semantics/move_semantics6.rs index 8d173fe..81b9658 100644 --- a/exercises/move_semantics/move_semantics6.rs +++ b/exercises/move_semantics/move_semantics6.rs @@ -5,24 +5,24 @@ // Execute `rustlings hint move_semantics6` or use the `hint` watch subcommand // for a hint. -// I AM NOT DONE + fn main() { let data = "Rust is great!".to_string(); - get_char(data); + get_char(&data); - string_uppercase(&data); + string_uppercase(data); } // Should not take ownership -fn get_char(data: String) -> char { +fn get_char(data: &String) -> char { data.chars().last().unwrap() } // Should take ownership -fn string_uppercase(mut data: &String) { - data = &data.to_uppercase(); +fn string_uppercase(mut data: String) { + data = data.to_uppercase(); println!("{}", data); } \ No newline at end of file diff --git a/exercises/options/options1.rs b/exercises/options/options1.rs index e131b48..b1785aa 100644 --- a/exercises/options/options1.rs +++ b/exercises/options/options1.rs @@ -3,7 +3,7 @@ // 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 @@ -13,7 +13,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 >23{ + None + }else{ + Some(0) + } } #[cfg(test)] @@ -34,6 +40,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.unwrap(), 5); } } diff --git a/exercises/quiz2.rs b/exercises/quiz2.rs index 29925ca..607afbd 100644 --- a/exercises/quiz2.rs +++ b/exercises/quiz2.rs @@ -20,7 +20,8 @@ // // No hints this time! -// I AM NOT DONE + + pub enum Command { Uppercase, @@ -32,11 +33,23 @@ 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! + // TODO: Complete the function body. You can do it! + let transformered = match command{ + Command::Uppercase=>string.to_uppercase(), + Command::Trim =>string.trim().to_string(), + Command::Append(n)=>{ + let mut owned_string =string.to_owned(); + for i in 0..*n{ + owned_string.push_str("bar"); + } + owned_string + }, + }; + output.push(transformered); } output } @@ -45,7 +58,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] diff --git a/exercises/quiz3.rs b/exercises/quiz3.rs index 3b01d31..9b0bf50 100644 --- a/exercises/quiz3.rs +++ b/exercises/quiz3.rs @@ -16,21 +16,22 @@ // // Execute `rustlings hint quiz3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE -pub struct ReportCard { - pub grade: f32, + +pub struct ReportCard { + pub grade: T, pub student_name: String, pub student_age: u8, } -impl ReportCard { +impl ReportCard { pub fn print(&self) -> String { format!("{} ({}) - achieved a grade of {}", &self.student_name, &self.student_age, &self.grade) } } + #[cfg(test)] mod tests { use super::*; @@ -52,7 +53,7 @@ mod tests { fn generate_alphabetic_report_card() { // TODO: Make sure to change the grade here after you finish the exercise. let report_card = ReportCard { - grade: 2.1, + grade: "A+", student_name: "Gary Plotter".to_string(), student_age: 11, }; diff --git a/exercises/smart_pointers/arc1.rs b/exercises/smart_pointers/arc1.rs index 3526ddc..7a6a7ad 100644 --- a/exercises/smart_pointers/arc1.rs +++ b/exercises/smart_pointers/arc1.rs @@ -21,7 +21,7 @@ // // Execute `rustlings hint arc1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE + #![forbid(unused_imports)] // Do not change this, (or the next) line. use std::sync::Arc; @@ -29,11 +29,11 @@ use std::thread; fn main() { let numbers: Vec<_> = (0..100u32).collect(); - let shared_numbers = // TODO + let shared_numbers = Arc::new(numbers);// TODO let mut joinhandles = Vec::new(); for offset in 0..8 { - let child_numbers = // TODO + let child_numbers = shared_numbers.clone();// TODO joinhandles.push(thread::spawn(move || { let sum: u32 = child_numbers.iter().filter(|&&n| n % 8 == offset).sum(); println!("Sum of offset {} is {}", offset, sum); diff --git a/exercises/smart_pointers/box1.rs b/exercises/smart_pointers/box1.rs index 513e7da..9cb1312 100644 --- a/exercises/smart_pointers/box1.rs +++ b/exercises/smart_pointers/box1.rs @@ -18,11 +18,11 @@ // // Execute `rustlings hint box1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE + #[derive(PartialEq, Debug)] pub enum List { - Cons(i32, List), + Cons(i32, Box), Nil, } @@ -35,11 +35,13 @@ fn main() { } pub fn create_empty_list() -> List { - todo!() + let empty = List::Nil; + empty } pub fn create_non_empty_list() -> List { - todo!() + let non_empty=List::Cons(1,Box::new(List::Nil)); + non_empty } #[cfg(test)] diff --git a/exercises/smart_pointers/cow1.rs b/exercises/smart_pointers/cow1.rs index 7ca9168..5e7ace2 100644 --- a/exercises/smart_pointers/cow1.rs +++ b/exercises/smart_pointers/cow1.rs @@ -12,7 +12,7 @@ // // Execute `rustlings hint cow1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE + use std::borrow::Cow; @@ -49,6 +49,8 @@ mod tests { let mut input = Cow::from(&slice[..]); match abs_all(&mut input) { // TODO + Cow::Borrowed(_)=>Ok(()), + _=>Err("Expected borrowed value"), } } @@ -61,6 +63,8 @@ mod tests { let mut input = Cow::from(slice); match abs_all(&mut input) { // TODO + Cow::Owned(_) => Ok(()), + _ => Err("Expected owned value"), } } @@ -73,6 +77,8 @@ mod tests { let mut input = Cow::from(slice); match abs_all(&mut input) { // TODO + Cow::Owned(_) => Ok(()), + _ => Err("Expected owned value"), } } } diff --git a/exercises/smart_pointers/rc1.rs b/exercises/smart_pointers/rc1.rs index ad3f1ce..1227b45 100644 --- a/exercises/smart_pointers/rc1.rs +++ b/exercises/smart_pointers/rc1.rs @@ -10,7 +10,7 @@ // // Execute `rustlings hint rc1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE + use std::rc::Rc; @@ -60,17 +60,17 @@ fn main() { jupiter.details(); // TODO - let saturn = Planet::Saturn(Rc::new(Sun {})); + let saturn = Planet::Saturn(Rc::clone(&sun)); println!("reference count = {}", Rc::strong_count(&sun)); // 7 references saturn.details(); // TODO - let uranus = Planet::Uranus(Rc::new(Sun {})); + let uranus = Planet::Uranus(Rc::clone(&sun)); println!("reference count = {}", Rc::strong_count(&sun)); // 8 references uranus.details(); // TODO - let neptune = Planet::Neptune(Rc::new(Sun {})); + let neptune = Planet::Neptune(Rc::clone(&sun)); println!("reference count = {}", Rc::strong_count(&sun)); // 9 references neptune.details(); @@ -92,12 +92,15 @@ fn main() { println!("reference count = {}", Rc::strong_count(&sun)); // 4 references // TODO + drop(earth); println!("reference count = {}", Rc::strong_count(&sun)); // 3 references // TODO + drop(venus); println!("reference count = {}", Rc::strong_count(&sun)); // 2 references // TODO + drop(mercury); println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference assert_eq!(Rc::strong_count(&sun), 1); diff --git a/exercises/strings/strings3.rs b/exercises/strings/strings3.rs index b29f932..cf2b339 100644 --- a/exercises/strings/strings3.rs +++ b/exercises/strings/strings3.rs @@ -3,21 +3,23 @@ // Execute `rustlings hint strings3` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE + fn trim_me(input: &str) -> String { // TODO: Remove whitespace from both ends of a string! - ??? + input.trim().to_string() } fn compose_me(input: &str) -> String { // TODO: Add " world!" to the string! There's multiple ways to do this! - ??? + let mut output= input.to_string(); + output.push_str(" world!"); + output } fn replace_me(input: &str) -> String { // TODO: Replace "cars" in the string with "balloons"! - ??? + input.replace("cars","balloons") } #[cfg(test)] diff --git a/exercises/strings/strings4.rs b/exercises/strings/strings4.rs index e8c54ac..133c1e4 100644 --- a/exercises/strings/strings4.rs +++ b/exercises/strings/strings4.rs @@ -7,7 +7,7 @@ // // No hints this time! -// I AM NOT DONE + fn string_slice(arg: &str) { println!("{}", arg); @@ -17,14 +17,14 @@ fn string(arg: String) { } fn main() { - ???("blue"); - ???("red".to_string()); - ???(String::from("hi")); - ???("rust is fun!".to_owned()); - ???("nice weather".into()); - ???(format!("Interpolation {}", "Station")); - ???(&String::from("abc")[0..1]); - ???(" hello there ".trim()); - ???("Happy Monday!".to_string().replace("Mon", "Tues")); - ???("mY sHiFt KeY iS sTiCkY".to_lowercase()); + string_slice("blue"); + string("red".to_string()); + string(String::from("hi")); + string("rust is fun!".to_owned()); + string_slice("nice weather".into()); + string(format!("Interpolation {}", "Station")); + string_slice(&String::from("abc")[0..1]); + string_slice(" hello there ".trim()); + string("Happy Monday!".to_string().replace("Mon", "Tues")); + string("mY sHiFt KeY iS sTiCkY".to_lowercase()); } diff --git a/exercises/structs/structs1.rs b/exercises/structs/structs1.rs index 5fa5821..3ea3a7e 100644 --- a/exercises/structs/structs1.rs +++ b/exercises/structs/structs1.rs @@ -5,13 +5,16 @@ // Execute `rustlings hint structs1` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE + struct ColorClassicStruct { // TODO: Something goes here + red:u8, + green:u8, + blue:u8, } -struct ColorTupleStruct(/* TODO: Something goes here */); +struct ColorTupleStruct(u8,u8,u8/* TODO: Something goes here */); #[derive(Debug)] struct UnitLikeStruct; @@ -23,7 +26,7 @@ mod tests { #[test] fn classic_c_structs() { // TODO: Instantiate a classic c struct! - // let green = + let green = ColorClassicStruct{red:0,green:255,blue:0}; assert_eq!(green.red, 0); assert_eq!(green.green, 255); @@ -33,7 +36,7 @@ mod tests { #[test] fn tuple_structs() { // TODO: Instantiate a tuple struct! - // let green = + let green = ColorTupleStruct(0,255,0); assert_eq!(green.0, 0); assert_eq!(green.1, 255); @@ -43,7 +46,7 @@ mod tests { #[test] fn unit_structs() { // TODO: Instantiate a unit-like struct! - // let unit_like_struct = + let unit_like_struct = UnitLikeStruct; let message = format!("{:?}s are fun!", unit_like_struct); assert_eq!(message, "UnitLikeStructs are fun!"); diff --git a/exercises/traits/traits3.rs b/exercises/traits/traits3.rs index 4e2b06b..55d17b0 100644 --- a/exercises/traits/traits3.rs +++ b/exercises/traits/traits3.rs @@ -8,10 +8,12 @@ // Execute `rustlings hint traits3` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE + pub trait Licensed { - fn licensing_info(&self) -> String; + fn licensing_info(&self) -> String{ + return String::from("Some information") + } } struct SomeSoftware { diff --git a/exercises/traits/traits4.rs b/exercises/traits/traits4.rs index 4bda3e5..20900de 100644 --- a/exercises/traits/traits4.rs +++ b/exercises/traits/traits4.rs @@ -7,7 +7,7 @@ // Execute `rustlings hint traits4` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE + pub trait Licensed { fn licensing_info(&self) -> String { @@ -23,7 +23,7 @@ impl Licensed for SomeSoftware {} impl Licensed for OtherSoftware {} // YOU MAY ONLY CHANGE THE NEXT LINE -fn compare_license_types(software: ??, software_two: ??) -> bool { +fn compare_license_types(software: T, software_two: U) -> bool { software.licensing_info() == software_two.licensing_info() } diff --git a/exercises/vecs/vecs1.rs b/exercises/vecs/vecs1.rs index 65e5fb5..d4cfb47 100644 --- a/exercises/vecs/vecs1.rs +++ b/exercises/vecs/vecs1.rs @@ -7,11 +7,10 @@ // // Execute `rustlings hint vecs1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE fn array_and_vec() -> ([i32; 4], Vec) { let a = [10, 20, 30, 40]; // a plain array - let v = // TODO: declare your vector here with the macro for vectors + let v = vec![10,20,30,40];// TODO: declare your vector here with the macro for vectors (a, v) } diff --git a/exercises/vecs/vecs2.rs b/exercises/vecs/vecs2.rs index 8a89141..cd11e2a 100644 --- a/exercises/vecs/vecs2.rs +++ b/exercises/vecs/vecs2.rs @@ -7,15 +7,16 @@ // // Execute `rustlings hint vecs2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE fn vec_loop(mut v: Vec) -> Vec { for element in v.iter_mut() { // TODO: Fill this up so that each element in the Vec `v` is // multiplied by 2. - ??? + + *element *= 2; } + println!("v:{:?}",v); // At this point, `v` should be equal to [4, 8, 12, 16, 20]. v } @@ -24,7 +25,7 @@ fn vec_map(v: &Vec) -> Vec { v.iter().map(|element| { // TODO: Do the same thing as above - but instead of mutating the // Vec, you can just return the new number! - ??? + element*2 }).collect() }