From 69f52eabf15705015634e1c57bbce639d0e9e0d7 Mon Sep 17 00:00:00 2001 From: XUKUNYUAN Date: Mon, 15 Apr 2024 13:54:41 +0800 Subject: [PATCH] first class --- exercises/functions/functions1.rs | 5 ++++- exercises/functions/functions2.rs | 3 +-- exercises/functions/functions3.rs | 3 +-- exercises/functions/functions4.rs | 3 +-- exercises/functions/functions5.rs | 3 +-- exercises/if/if1.rs | 7 ++++++- exercises/if/if2.rs | 5 +++-- exercises/if/if3.rs | 5 ++--- exercises/move_semantics/move_semantics1.rs | 3 +-- exercises/move_semantics/move_semantics2.rs | 3 +-- exercises/move_semantics/move_semantics3.rs | 3 +-- exercises/move_semantics/move_semantics4.rs | 7 +++---- exercises/move_semantics/move_semantics5.rs | 3 +-- exercises/move_semantics/move_semantics6.rs | 9 ++++----- exercises/primitive_types/primitive_types1.rs | 3 +-- exercises/primitive_types/primitive_types2.rs | 3 +-- exercises/primitive_types/primitive_types3.rs | 3 +-- exercises/primitive_types/primitive_types4.rs | 3 +-- exercises/primitive_types/primitive_types5.rs | 3 +-- exercises/primitive_types/primitive_types6.rs | 3 +-- exercises/quiz1.rs | 10 ++++++++-- exercises/variables/variables1.rs | 3 +-- exercises/variables/variables2.rs | 3 +-- exercises/variables/variables3.rs | 3 +-- exercises/variables/variables4.rs | 3 +-- exercises/variables/variables5.rs | 3 +-- exercises/variables/variables6.rs | 3 +-- exercises/vecs/vecs1.rs | 3 +-- exercises/vecs/vecs2.rs | 5 ++--- 29 files changed, 53 insertions(+), 63 deletions(-) diff --git a/exercises/functions/functions1.rs b/exercises/functions/functions1.rs index 40ed9a0..2816f0a 100644 --- a/exercises/functions/functions1.rs +++ b/exercises/functions/functions1.rs @@ -3,8 +3,11 @@ // Execute `rustlings hint functions1` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE fn main() { call_me(); } + +fn call_me() { + +} diff --git a/exercises/functions/functions2.rs b/exercises/functions/functions2.rs index 5154f34..70610cd 100644 --- a/exercises/functions/functions2.rs +++ b/exercises/functions/functions2.rs @@ -3,13 +3,12 @@ // Execute `rustlings hint functions2` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE fn main() { call_me(3); } -fn call_me(num:) { +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 74f44d6..0a01731 100644 --- a/exercises/functions/functions3.rs +++ b/exercises/functions/functions3.rs @@ -3,10 +3,9 @@ // Execute `rustlings hint functions3` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE fn main() { - call_me(); + call_me(3); } fn call_me(num: u32) { diff --git a/exercises/functions/functions4.rs b/exercises/functions/functions4.rs index 77c4b2a..96891ce 100644 --- a/exercises/functions/functions4.rs +++ b/exercises/functions/functions4.rs @@ -8,14 +8,13 @@ // Execute `rustlings hint functions4` or use the `hint` watch subcommand for a // hint. -// 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 f1b63f4..a9dd1b5 100644 --- a/exercises/functions/functions5.rs +++ b/exercises/functions/functions5.rs @@ -3,7 +3,6 @@ // Execute `rustlings hint functions5` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE fn main() { let answer = square(3); @@ -11,5 +10,5 @@ fn main() { } fn square(num: i32) -> i32 { - num * num; + num * num } diff --git a/exercises/if/if1.rs b/exercises/if/if1.rs index d8108a0..4b8ad58 100644 --- a/exercises/if/if1.rs +++ b/exercises/if/if1.rs @@ -2,13 +2,18 @@ // // Execute `rustlings hint if1` or use the `hint` watch subcommand for a hint. -// 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 + + if a > b { + a + } else { + b + } } // Don't mind this for now :) diff --git a/exercises/if/if2.rs b/exercises/if/if2.rs index f512f13..40c0780 100644 --- a/exercises/if/if2.rs +++ b/exercises/if/if2.rs @@ -5,13 +5,14 @@ // // Execute `rustlings hint if2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE pub fn foo_if_fizz(fizzish: &str) -> &str { if fizzish == "fizz" { "foo" + } else if fizzish == "fuzz" { + "bar" } else { - 1 + "baz" } } diff --git a/exercises/if/if3.rs b/exercises/if/if3.rs index 73a7025..ea3a1b2 100644 --- a/exercises/if/if3.rs +++ b/exercises/if/if3.rs @@ -2,17 +2,16 @@ // // Execute `rustlings hint if3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE pub fn animal_habitat(animal: &str) -> &'static str { let identifier = if animal == "crab" { 1 } else if animal == "gopher" { - 2.0 + 2 } else if animal == "snake" { 3 } else { - "Unknown" + 0 }; // DO NOT CHANGE THIS STATEMENT BELOW diff --git a/exercises/move_semantics/move_semantics1.rs b/exercises/move_semantics/move_semantics1.rs index 710d20d..eb65088 100644 --- a/exercises/move_semantics/move_semantics1.rs +++ b/exercises/move_semantics/move_semantics1.rs @@ -3,12 +3,11 @@ // Execute `rustlings hint move_semantics1` or use the `hint` watch subcommand // for a hint. -// I AM NOT DONE fn main() { let vec0 = Vec::new(); - let vec1 = fill_vec(vec0); + let mut vec1 = fill_vec(vec0); println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); diff --git a/exercises/move_semantics/move_semantics2.rs b/exercises/move_semantics/move_semantics2.rs index 72d37fa..aa54b90 100644 --- a/exercises/move_semantics/move_semantics2.rs +++ b/exercises/move_semantics/move_semantics2.rs @@ -7,12 +7,11 @@ // Execute `rustlings hint move_semantics2` or use the `hint` watch subcommand // for a hint. -// I AM NOT DONE fn main() { let vec0 = Vec::new(); - let mut vec1 = fill_vec(vec0); + let mut vec1 = fill_vec(vec0.clone()); println!("{} has length {}, with contents: `{:?}`", "vec0", vec0.len(), vec0); diff --git a/exercises/move_semantics/move_semantics3.rs b/exercises/move_semantics/move_semantics3.rs index ea21493..02335ce 100644 --- a/exercises/move_semantics/move_semantics3.rs +++ b/exercises/move_semantics/move_semantics3.rs @@ -6,7 +6,6 @@ // Execute `rustlings hint move_semantics3` or use the `hint` watch subcommand // for a hint. -// I AM NOT DONE fn main() { let vec0 = Vec::new(); @@ -20,7 +19,7 @@ fn main() { println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); } -fn fill_vec(vec: Vec) -> Vec { +fn fill_vec(mut vec: Vec) -> Vec { vec.push(22); vec.push(44); vec.push(66); diff --git a/exercises/move_semantics/move_semantics4.rs b/exercises/move_semantics/move_semantics4.rs index 75a3b6b..9fc1e2e 100644 --- a/exercises/move_semantics/move_semantics4.rs +++ b/exercises/move_semantics/move_semantics4.rs @@ -7,12 +7,11 @@ // 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 +22,7 @@ fn main() { // `fill_vec()` no longer takes `vec: Vec` as argument fn fill_vec() -> Vec { - let mut vec = vec; + let mut 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 68db09e..4d70062 100644 --- a/exercises/move_semantics/move_semantics5.rs +++ b/exercises/move_semantics/move_semantics5.rs @@ -6,13 +6,12 @@ // 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); } diff --git a/exercises/move_semantics/move_semantics6.rs b/exercises/move_semantics/move_semantics6.rs index cace4ca..7786194 100644 --- a/exercises/move_semantics/move_semantics6.rs +++ b/exercises/move_semantics/move_semantics6.rs @@ -5,14 +5,13 @@ // 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.clone()); - string_uppercase(&data); + string_uppercase(data); } // Should not take ownership @@ -21,8 +20,8 @@ fn get_char(data: String) -> char { } // Should take ownership -fn string_uppercase(mut data: &String) { - data = &data.to_uppercase(); +fn string_uppercase(mut data: String) { + data.to_uppercase(); println!("{}", data); } diff --git a/exercises/primitive_types/primitive_types1.rs b/exercises/primitive_types/primitive_types1.rs index e1cf52a..b069a8d 100644 --- a/exercises/primitive_types/primitive_types1.rs +++ b/exercises/primitive_types/primitive_types1.rs @@ -6,7 +6,6 @@ // Execute `rustlings hint primitive_types1` or use the `hint` watch subcommand // for a hint. -// I AM NOT DONE fn main() { // Booleans (`bool`) @@ -16,7 +15,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 fcc9705..fee0ef1 100644 --- a/exercises/primitive_types/primitive_types2.rs +++ b/exercises/primitive_types/primitive_types2.rs @@ -6,7 +6,6 @@ // Execute `rustlings hint primitive_types2` or use the `hint` watch subcommand // for a hint. -// I AM NOT DONE fn main() { // Characters (`char`) @@ -22,7 +21,7 @@ fn main() { println!("Neither alphabetic nor numeric!"); } - let // Finish this line like the example! What's your favorite character? + 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 06a7a62..a6635b6 100644 --- a/exercises/primitive_types/primitive_types3.rs +++ b/exercises/primitive_types/primitive_types3.rs @@ -5,10 +5,9 @@ // Execute `rustlings hint primitive_types3` or use the `hint` watch subcommand // for a hint. -// I AM NOT DONE fn main() { - let a = ??? + let a = [0; 1000]; 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 d44d877..9564c7a 100644 --- a/exercises/primitive_types/primitive_types4.rs +++ b/exercises/primitive_types/primitive_types4.rs @@ -5,13 +5,12 @@ // Execute `rustlings hint primitive_types4` or use the `hint` watch subcommand // for a hint. -// 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]; assert_eq!([2, 3, 4], nice_slice) } diff --git a/exercises/primitive_types/primitive_types5.rs b/exercises/primitive_types/primitive_types5.rs index f646986..fb754fa 100644 --- a/exercises/primitive_types/primitive_types5.rs +++ b/exercises/primitive_types/primitive_types5.rs @@ -5,11 +5,10 @@ // Execute `rustlings hint primitive_types5` or use the `hint` watch subcommand // for a hint. -// I AM NOT DONE fn main() { let cat = ("Furry McFurson", 3.5); - let /* your pattern here */ = cat; + let /* your pattern here */(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 07cc46c..d556d08 100644 --- a/exercises/primitive_types/primitive_types6.rs +++ b/exercises/primitive_types/primitive_types6.rs @@ -6,13 +6,12 @@ // Execute `rustlings hint primitive_types6` or use the `hint` watch subcommand // for a hint. -// I AM NOT DONE #[test] fn indexing_tuple() { let numbers = (1, 2, 3); // Replace below ??? with the tuple indexing syntax. - let second = ???; + let second = numbers.1; assert_eq!(2, second, "This is not the 2nd number in the tuple!") diff --git a/exercises/quiz1.rs b/exercises/quiz1.rs index a9904b8..379a969 100644 --- a/exercises/quiz1.rs +++ b/exercises/quiz1.rs @@ -13,10 +13,16 @@ // // No hints this time ;) -// I AM NOT DONE // Put your function here! -// fn calculate_price_of_apples { +fn calculate_price_of_apples(number: i32) -> i32 { + + if number > 40 { + number + } else { + number * 2 + } +} // Don't modify this function! #[test] diff --git a/exercises/variables/variables1.rs b/exercises/variables/variables1.rs index b3e089a..469fd4e 100644 --- a/exercises/variables/variables1.rs +++ b/exercises/variables/variables1.rs @@ -5,9 +5,8 @@ // Execute `rustlings hint variables1` or use the `hint` watch subcommand for a // hint. -// 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 e1c23ed..ff9a781 100644 --- a/exercises/variables/variables2.rs +++ b/exercises/variables/variables2.rs @@ -3,10 +3,9 @@ // Execute `rustlings hint variables2` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE fn main() { - let x; + let x = 10; if x == 10 { println!("x is ten!"); } else { diff --git a/exercises/variables/variables3.rs b/exercises/variables/variables3.rs index 86bed41..a1bc9bc 100644 --- a/exercises/variables/variables3.rs +++ b/exercises/variables/variables3.rs @@ -3,9 +3,8 @@ // Execute `rustlings hint variables3` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE fn main() { - let x: i32; + let x: i32 = 10; println!("Number {}", x); } diff --git a/exercises/variables/variables4.rs b/exercises/variables/variables4.rs index 5394f39..2d056da 100644 --- a/exercises/variables/variables4.rs +++ b/exercises/variables/variables4.rs @@ -3,10 +3,9 @@ // Execute `rustlings hint variables4` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE fn main() { - let x = 3; + let mut x = 3; println!("Number {}", x); x = 5; // don't change this line println!("Number {}", x); diff --git a/exercises/variables/variables5.rs b/exercises/variables/variables5.rs index a29b38b..2a312ac 100644 --- a/exercises/variables/variables5.rs +++ b/exercises/variables/variables5.rs @@ -3,11 +3,10 @@ // Execute `rustlings hint variables5` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE fn main() { let number = "T-H-R-E-E"; // don't change this line println!("Spell a Number : {}", number); - number = 3; // don't rename this variable + let number = 3; // don't rename this variable println!("Number plus two is : {}", number + 2); } diff --git a/exercises/variables/variables6.rs b/exercises/variables/variables6.rs index 853183b..052a2f6 100644 --- a/exercises/variables/variables6.rs +++ b/exercises/variables/variables6.rs @@ -3,9 +3,8 @@ // Execute `rustlings hint variables6` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE -const NUMBER = 3; +const NUMBER: i32 = 3; fn main() { println!("Number {}", NUMBER); } diff --git a/exercises/vecs/vecs1.rs b/exercises/vecs/vecs1.rs index 65b7a7f..8b7d8e1 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 e92c970..e6eb37a 100644 --- a/exercises/vecs/vecs2.rs +++ b/exercises/vecs/vecs2.rs @@ -7,13 +7,12 @@ // // 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; } // At this point, `v` should be equal to [4, 8, 12, 16, 20]. @@ -24,7 +23,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() }