From dad89797140201098416c1821f6b029357e5ab40 Mon Sep 17 00:00:00 2001 From: Sriharsha Madala Date: Sun, 19 May 2024 22:23:14 -0700 Subject: [PATCH] first snapshot of progress. --- exercises/functions/functions1.rs | 2 +- exercises/functions/functions2.rs | 5 ++--- exercises/functions/functions3.rs | 4 +--- exercises/functions/functions4.rs | 4 +--- exercises/functions/functions5.rs | 4 +--- exercises/if/if1.rs | 3 +-- exercises/modules/modules1.rs | 3 +-- exercises/modules/modules2.rs | 7 +++---- exercises/primitive_types/primitive_types1.rs | 4 +--- exercises/primitive_types/primitive_types2.rs | 5 ++--- exercises/primitive_types/primitive_types3.rs | 9 ++++++--- exercises/primitive_types/primitive_types4.rs | 7 ++++--- exercises/primitive_types/primitive_types5.rs | 4 +--- exercises/primitive_types/primitive_types6.rs | 4 +--- exercises/strings/strings1.rs | 5 ++--- exercises/strings/strings2.rs | 4 +--- exercises/structs/structs1.rs | 18 +++++++----------- exercises/structs/structs2.rs | 4 +--- exercises/test1.rs | 5 +++-- exercises/test3.rs | 5 ++--- exercises/tests/tests2.rs | 4 +--- exercises/tests/tests3.rs | 9 ++++++--- exercises/variables/variables1.rs | 4 +--- exercises/variables/variables2.rs | 8 +++++--- exercises/variables/variables3.rs | 4 +--- exercises/variables/variables4.rs | 4 +--- 26 files changed, 57 insertions(+), 82 deletions(-) diff --git a/exercises/functions/functions1.rs b/exercises/functions/functions1.rs index 31125278..598d21e1 100644 --- a/exercises/functions/functions1.rs +++ b/exercises/functions/functions1.rs @@ -1,7 +1,7 @@ // functions1.rs // Make me compile! Execute `rustlings hint functions1` for hints :) -// I AM NOT DONE +fn call_me () {} fn main() { call_me(); diff --git a/exercises/functions/functions2.rs b/exercises/functions/functions2.rs index 108ba38b..58837165 100644 --- a/exercises/functions/functions2.rs +++ b/exercises/functions/functions2.rs @@ -1,13 +1,12 @@ // functions2.rs // Make me compile! Execute `rustlings hint functions2` for hints :) -// I AM NOT DONE - fn main() { call_me(3); } -fn call_me(num) { +// NOTE: It is interesting that the function is defined below main but main can still call this. +fn call_me(num: i32) { for i in 0..num { println!("Ring! Call number {}", i + 1); } diff --git a/exercises/functions/functions3.rs b/exercises/functions/functions3.rs index e3c1bf73..dfcb0f79 100644 --- a/exercises/functions/functions3.rs +++ b/exercises/functions/functions3.rs @@ -1,10 +1,8 @@ // functions3.rs // Make me compile! Execute `rustlings hint functions3` for hints :) -// I AM NOT DONE - fn main() { - call_me(); + call_me(10); } fn call_me(num: i32) { diff --git a/exercises/functions/functions4.rs b/exercises/functions/functions4.rs index 6bf46f08..55566cf5 100644 --- a/exercises/functions/functions4.rs +++ b/exercises/functions/functions4.rs @@ -4,14 +4,12 @@ // This store is having a sale where if the price is an even number, you get // 10 (money unit) off, but if it's an odd number, it's 3 (money unit) less. -// I AM NOT DONE - fn main() { let original_price = 51; println!("Your sale price is {}", sale_price(original_price)); } -fn sale_price(price: i32) -> { +fn sale_price(price: i32) -> i32 { if is_even(price) { price - 10 } else { diff --git a/exercises/functions/functions5.rs b/exercises/functions/functions5.rs index d22aa6c8..059e9295 100644 --- a/exercises/functions/functions5.rs +++ b/exercises/functions/functions5.rs @@ -1,13 +1,11 @@ // functions5.rs // Make me compile! Execute `rustlings hint functions5` for hints :) -// I AM NOT DONE - fn main() { let answer = square(3); println!("The answer is {}", answer); } fn square(num: i32) -> i32 { - num * num; + num * num } diff --git a/exercises/if/if1.rs b/exercises/if/if1.rs index 90867545..10c3670f 100644 --- a/exercises/if/if1.rs +++ b/exercises/if/if1.rs @@ -1,13 +1,12 @@ // if1.rs -// I AM NOT DONE - pub fn bigger(a: i32, b: i32) -> i32 { // Complete this function to return the bigger number! // Do not use: // - another function call // - additional variables // Execute `rustlings hint if1` for hints + if a >= b { a } else { b } } // Don't mind this for now :) diff --git a/exercises/modules/modules1.rs b/exercises/modules/modules1.rs index 812dfeef..c76b0586 100644 --- a/exercises/modules/modules1.rs +++ b/exercises/modules/modules1.rs @@ -1,10 +1,9 @@ // modules1.rs // Make me compile! Execute `rustlings hint modules1` for hints :) -// I AM NOT DONE mod sausage_factory { - fn make_sausage() { + pub fn make_sausage() { println!("sausage!"); } } diff --git a/exercises/modules/modules2.rs b/exercises/modules/modules2.rs index fde439d1..5130dfda 100644 --- a/exercises/modules/modules2.rs +++ b/exercises/modules/modules2.rs @@ -1,11 +1,10 @@ // modules2.rs // Make me compile! Execute `rustlings hint modules2` for hints :) -// I AM NOT DONE - mod delicious_snacks { - use self::fruits::PEAR as fruit; - use self::veggies::CUCUMBER as veggie; + // Just like items, name bindings as a result of use is private by default. + 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/primitive_types/primitive_types1.rs b/exercises/primitive_types/primitive_types1.rs index 09121392..91734c69 100644 --- a/exercises/primitive_types/primitive_types1.rs +++ b/exercises/primitive_types/primitive_types1.rs @@ -2,8 +2,6 @@ // Fill in the rest of the line that has code missing! // No hints, there's no tricks, just get used to typing these :) -// I AM NOT DONE - fn main() { // Booleans (`bool`) @@ -12,7 +10,7 @@ fn main() { println!("Good morning!"); } - let // Finish the rest of this line like the example! Or make it be false! + let is_evening = false; // Finish the rest of this line like the example! Or make it be false! if is_evening { println!("Good evening!"); } diff --git a/exercises/primitive_types/primitive_types2.rs b/exercises/primitive_types/primitive_types2.rs index 6576a4d5..267a8c03 100644 --- a/exercises/primitive_types/primitive_types2.rs +++ b/exercises/primitive_types/primitive_types2.rs @@ -2,8 +2,6 @@ // Fill in the rest of the line that has code missing! // No hints, there's no tricks, just get used to typing these :) -// I AM NOT DONE - fn main() { // Characters (`char`) @@ -16,7 +14,8 @@ fn main() { println!("Neither alphabetic nor numeric!"); } - let // Finish this line like the example! What's your favorite character? + // This is amazing; Rust handles Unicode so well; It even understands alphabets vs numbers in other languages; + let your_character = 'అ'; // Finish this line like the example! What's your favorite character? // Try a letter, try a number, try a special character, try a character // from a different language than your own, try an emoji! if your_character.is_alphabetic() { diff --git a/exercises/primitive_types/primitive_types3.rs b/exercises/primitive_types/primitive_types3.rs index dfd6351c..7ed73809 100644 --- a/exercises/primitive_types/primitive_types3.rs +++ b/exercises/primitive_types/primitive_types3.rs @@ -2,10 +2,13 @@ // Create an array with at least 100 elements in it where the ??? is. // Execute `rustlings hint primitive_types3` for hints! -// I AM NOT DONE - fn main() { - let a = ??? + // Initialize the array to zeroes of size 100 + // Copy trait needs to be implemented by the type. In this case its primitive type. + let a = [0; 100]; + // Explicitly initialize each element + // let a = [0, 0, 0]; + // let a: [Option; 3] = [None; 3]; alternatively let a: [Option; 3] = Default::default(); if string doesn't support copy trait. if a.len() >= 100 { println!("Wow, that's a big array!"); diff --git a/exercises/primitive_types/primitive_types4.rs b/exercises/primitive_types/primitive_types4.rs index 10b553e9..0e10b3de 100644 --- a/exercises/primitive_types/primitive_types4.rs +++ b/exercises/primitive_types/primitive_types4.rs @@ -2,13 +2,14 @@ // Get a slice out of Array a where the ??? is so that the test passes. // Execute `rustlings hint primitive_types4` for hints!! -// I AM NOT DONE - #[test] fn slice_out_of_array() { let a = [1, 2, 3, 4, 5]; - let nice_slice = ??? + let nice_slice = &a[1..4]; + // XXX Reason you don't need to add '&' infront of the slice [2,3,4] is + // deref trait is implicit in the current context. + // If we define a new data type and define our own deref trait, the same behavior can be expected. assert_eq!([2, 3, 4], nice_slice) } diff --git a/exercises/primitive_types/primitive_types5.rs b/exercises/primitive_types/primitive_types5.rs index 680d8d23..85f42cfc 100644 --- a/exercises/primitive_types/primitive_types5.rs +++ b/exercises/primitive_types/primitive_types5.rs @@ -2,11 +2,9 @@ // Destructure the `cat` tuple so that the println will work. // Execute `rustlings hint primitive_types5` for hints! -// I AM NOT DONE - fn main() { let cat = ("Furry McFurson", 3.5); - let /* your pattern here */ = cat; + let (name, age) = cat; println!("{} is {} years old.", name, age); } diff --git a/exercises/primitive_types/primitive_types6.rs b/exercises/primitive_types/primitive_types6.rs index 2bc817e9..c8d010a3 100644 --- a/exercises/primitive_types/primitive_types6.rs +++ b/exercises/primitive_types/primitive_types6.rs @@ -3,9 +3,7 @@ // You can put this right into the `println!` where the ??? is. // Execute `rustlings hint primitive_types6` for hints! -// I AM NOT DONE - fn main() { let numbers = (1, 2, 3); - println!("The second number is {}", ???); + println!("The second number is {}", numbers.1); } diff --git a/exercises/strings/strings1.rs b/exercises/strings/strings1.rs index 80902444..dbdba9b5 100644 --- a/exercises/strings/strings1.rs +++ b/exercises/strings/strings1.rs @@ -2,13 +2,12 @@ // Make me compile without changing the function signature! // Execute `rustlings hint strings1` for hints ;) -// I AM NOT DONE - fn main() { let answer = current_favorite_color(); println!("My current favorite color is {}", answer); } fn current_favorite_color() -> String { - "blue" + "blue".to_string() + //String::from("blue") } diff --git a/exercises/strings/strings2.rs b/exercises/strings/strings2.rs index 5a2ce74a..a048f882 100644 --- a/exercises/strings/strings2.rs +++ b/exercises/strings/strings2.rs @@ -2,11 +2,9 @@ // Make me compile without changing the function signature! // Execute `rustlings hint strings2` for hints :) -// I AM NOT DONE - fn main() { let word = String::from("green"); // Try not changing this line :) - if is_a_color_word(word) { + if is_a_color_word(&word) { println!("That is a color word I know!"); } else { println!("That is not a color word I know."); diff --git a/exercises/structs/structs1.rs b/exercises/structs/structs1.rs index 6d0b2f49..603ad300 100644 --- a/exercises/structs/structs1.rs +++ b/exercises/structs/structs1.rs @@ -1,13 +1,12 @@ // structs1.rs // Address all the TODOs to make the tests pass! -// I AM NOT DONE - -struct ColorClassicStruct { - // TODO: Something goes here +struct ColorClassicStruct<'a> { + name: &'a str, + hex: &'a str, } -struct ColorTupleStruct(/* TODO: Something goes here */); +struct ColorTupleStruct<'a> (&'a str, &'a str); #[derive(Debug)] struct UnitStruct; @@ -18,8 +17,7 @@ mod tests { #[test] fn classic_c_structs() { - // TODO: Instantiate a classic c struct! - // let green = + let green = ColorClassicStruct {name: "green", hex: "#00FF00"}; assert_eq!(green.name, "green"); assert_eq!(green.hex, "#00FF00"); @@ -27,8 +25,7 @@ mod tests { #[test] fn tuple_structs() { - // TODO: Instantiate a tuple struct! - // let green = + let green = ("green", "#00FF00"); assert_eq!(green.0, "green"); assert_eq!(green.1, "#00FF00"); @@ -36,8 +33,7 @@ mod tests { #[test] fn unit_structs() { - // TODO: Instantiate a unit struct! - // let unit_struct = + let unit_struct = UnitStruct; let message = format!("{:?}s are fun!", unit_struct); assert_eq!(message, "UnitStructs are fun!"); diff --git a/exercises/structs/structs2.rs b/exercises/structs/structs2.rs index 0699137c..034a3161 100644 --- a/exercises/structs/structs2.rs +++ b/exercises/structs/structs2.rs @@ -2,8 +2,6 @@ // Address all the TODOs to make the tests pass! // No hints, just do it! -// I AM NOT DONE - #[derive(Debug)] struct Order { name: String, @@ -35,7 +33,7 @@ mod tests { fn your_order() { let order_template = create_order_template(); // TODO: Create your own order using the update syntax and template above! - // let your_order = + let your_order = Order {name: String::from("Hacker in Rust"), count: 1, ..order_template}; assert_eq!(your_order.name, "Hacker in Rust"); assert_eq!(your_order.year, order_template.year); assert_eq!(your_order.made_by_phone, order_template.made_by_phone); diff --git a/exercises/test1.rs b/exercises/test1.rs index 3e13ce55..71ff826e 100644 --- a/exercises/test1.rs +++ b/exercises/test1.rs @@ -7,10 +7,11 @@ // more than 40 at once, each apple only costs 1! Write a function that calculates // the price of an order of apples given the order amount. No hints this time! -// I AM NOT DONE - // Put your function here! // fn ..... { +fn calculate_apple_price (order: u32) -> u32 { + if order > 40 { order } else { order * 2 } +} // Don't modify this function! #[test] diff --git a/exercises/test3.rs b/exercises/test3.rs index f94c36f4..694b3508 100644 --- a/exercises/test3.rs +++ b/exercises/test3.rs @@ -7,8 +7,6 @@ // we expect to get when we call `times_two` with a negative number. // No hints, you can do this :) -// I AM NOT DONE - pub fn times_two(num: i32) -> i32 { num * 2 } @@ -19,11 +17,12 @@ mod tests { #[test] fn returns_twice_of_positive_numbers() { - assert_eq!(times_two(4), ???); + assert_eq!(times_two(4), 8); } #[test] fn returns_twice_of_negative_numbers() { // TODO write an assert for `times_two(-4)` + assert_eq!(times_two(-4), -8); } } diff --git a/exercises/tests/tests2.rs b/exercises/tests/tests2.rs index 0d981ad1..6bfcd091 100644 --- a/exercises/tests/tests2.rs +++ b/exercises/tests/tests2.rs @@ -2,12 +2,10 @@ // This test has a problem with it -- make the test compile! Make the test // pass! Make the test fail! Execute `rustlings hint tests2` for hints :) -// I AM NOT DONE - #[cfg(test)] mod tests { #[test] fn you_can_assert_eq() { - assert_eq!(); + assert_eq!(1, 1); } } diff --git a/exercises/tests/tests3.rs b/exercises/tests/tests3.rs index 693b8aa5..86a160c1 100644 --- a/exercises/tests/tests3.rs +++ b/exercises/tests/tests3.rs @@ -4,8 +4,6 @@ // we expect to get when we call `is_even(5)`. // Execute `rustlings hint tests3` for hints :) -// I AM NOT DONE - pub fn is_even(num: i32) -> bool { num % 2 == 0 } @@ -16,6 +14,11 @@ mod tests { #[test] fn is_true_when_even() { - assert!(); + assert!(is_even(2)); + } + + #[test] + fn negative_test() { + assert!(!is_even(5)); } } diff --git a/exercises/variables/variables1.rs b/exercises/variables/variables1.rs index 4a3af73c..db85e7e4 100644 --- a/exercises/variables/variables1.rs +++ b/exercises/variables/variables1.rs @@ -6,9 +6,7 @@ // even after you already figured it out. If you got everything working and // feel ready for the next exercise, remove the `I AM NOT DONE` comment below. -// I AM NOT DONE - fn main() { - x = 5; + let x = 5; println!("x has the value {}", x); } diff --git a/exercises/variables/variables2.rs b/exercises/variables/variables2.rs index 7774a8fb..03a65800 100644 --- a/exercises/variables/variables2.rs +++ b/exercises/variables/variables2.rs @@ -1,10 +1,12 @@ // variables2.rs // Make me compile! Execute the command `rustlings hint variables2` if you want a hint :) -// I AM NOT DONE - fn main() { - let x; + // Just specifying the type is not sufficient as Rust doesn't allow uninitalized variables to be used later.alloc + // Just specifying the value is fine as type is inferred, + // but if it doesn't match with the value being compared later, we get error. let x = 10.0; + // You can always specify both. + let x = 10; if x == 10 { println!("Ten!"); } else { diff --git a/exercises/variables/variables3.rs b/exercises/variables/variables3.rs index 07b1a521..2bd4d3c2 100644 --- a/exercises/variables/variables3.rs +++ b/exercises/variables/variables3.rs @@ -1,10 +1,8 @@ // variables3.rs // Make me compile! Execute the command `rustlings hint variables3` if you want a hint :) -// I AM NOT DONE - fn main() { - let x = 3; + let mut x = 3; println!("Number {}", x); x = 5; println!("Number {}", x); diff --git a/exercises/variables/variables4.rs b/exercises/variables/variables4.rs index 77f1e9ab..011ba7ec 100644 --- a/exercises/variables/variables4.rs +++ b/exercises/variables/variables4.rs @@ -1,9 +1,7 @@ // variables4.rs // Make me compile! Execute the command `rustlings hint variables4` if you want a hint :) -// I AM NOT DONE - fn main() { - let x: i32; + let x: i32 = 42; println!("Number {}", x); }