Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Ming-XX committed Jan 21, 2025
1 parent 82e723e commit 024fc68
Show file tree
Hide file tree
Showing 30 changed files with 199 additions and 107 deletions.
4 changes: 2 additions & 2 deletions exercises/generics/generics1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
10 changes: 5 additions & 5 deletions exercises/generics/generics2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
value: T,
}

impl Wrapper {
pub fn new(value: u32) -> Self {
impl<T> Wrapper<T> {
pub fn new(value: T) -> Self {
Wrapper { value }
}
}
Expand Down
8 changes: 6 additions & 2 deletions exercises/hashmaps/hashmaps1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, u32> {
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
}
Expand Down
5 changes: 4 additions & 1 deletion exercises/hashmaps/hashmaps2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -40,6 +40,9 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
// 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);
}
}
}

Expand Down
27 changes: 26 additions & 1 deletion exercises/hashmaps/hashmaps3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -39,6 +39,31 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
// 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
}
Expand Down
10 changes: 5 additions & 5 deletions exercises/iterators/iterators1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
28 changes: 23 additions & 5 deletions exercises/iterators/iterators2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -15,24 +15,42 @@ 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.
// Apply the `capitalize_first` function to a slice of string slices.
// Return a vector of strings.
// ["hello", "world"] -> ["Hello", "World"]
pub fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
vec![]
let mut new_words=Vec::new();
for i in 0..words.len(){
new_words.push(capitalize_first(words[i]))
}
new_words
}

// Step 3.
// Apply the `capitalize_first` function again to a slice of string slices.
// 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)]
Expand Down
4 changes: 2 additions & 2 deletions exercises/lifetimes/lifetimes1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 7 additions & 5 deletions exercises/lifetimes/lifetimes2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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);


}
8 changes: 4 additions & 4 deletions exercises/lifetimes/lifetimes3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions exercises/macros/macros1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
() => {
Expand All @@ -12,5 +12,5 @@ macro_rules! my_macro {
}

fn main() {
my_macro();
my_macro!();
}
10 changes: 6 additions & 4 deletions exercises/macros/macros2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!();
}
6 changes: 3 additions & 3 deletions exercises/modules/modules2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
8 changes: 4 additions & 4 deletions exercises/move_semantics/move_semantics4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -23,7 +23,7 @@ fn main() {

// `fill_vec()` no longer takes `vec: Vec<i32>` as argument
fn fill_vec() -> Vec<i32> {
let mut vec = vec;
let mut vec:Vec<i32> = Vec::new();

vec.push(22);
vec.push(44);
Expand Down
4 changes: 2 additions & 2 deletions exercises/move_semantics/move_semantics5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
12 changes: 6 additions & 6 deletions exercises/move_semantics/move_semantics6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Loading

0 comments on commit 024fc68

Please sign in to comment.