Skip to content

Commit 5a142e1

Browse files
committed
update
1 parent 4dadc9c commit 5a142e1

File tree

13 files changed

+55
-40
lines changed

13 files changed

+55
-40
lines changed

exercises/iterators/iterators1.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@
99
// Execute `rustlings hint iterators1` or use the `hint` watch subcommand for a
1010
// hint.
1111

12-
// I AM NOT DONE
12+
1313

1414
fn main() {
1515
let my_fav_fruits = vec!["banana", "custard apple", "avocado", "peach", "raspberry"];
1616

17-
let mut my_iterable_fav_fruits = ???; // TODO: Step 1
17+
let mut my_iterable_fav_fruits = my_fav_fruits.iter(); // TODO: Step 1
1818

1919
assert_eq!(my_iterable_fav_fruits.next(), Some(&"banana"));
20-
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 2
20+
assert_eq!(my_iterable_fav_fruits.next(), Some(&"custard apple")); // TODO: Step 2
2121
assert_eq!(my_iterable_fav_fruits.next(), Some(&"avocado"));
22-
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 3
22+
assert_eq!(my_iterable_fav_fruits.next(), Some(&"peach")); // TODO: Step 3
2323
assert_eq!(my_iterable_fav_fruits.next(), Some(&"raspberry"));
24-
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 4
24+
assert_eq!(my_iterable_fav_fruits.next(), None); // TODO: Step 4
2525
}

exercises/iterators/iterators2.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// Execute `rustlings hint iterators2` or use the `hint` watch subcommand for a
77
// hint.
88

9-
// I AM NOT DONE
9+
1010

1111
// Step 1.
1212
// Complete the `capitalize_first` function.
@@ -15,7 +15,7 @@ pub fn capitalize_first(input: &str) -> String {
1515
let mut c = input.chars();
1616
match c.next() {
1717
None => String::new(),
18-
Some(first) => ???,
18+
Some(first) => first.to_uppercase().to_string() + c.as_str(),
1919
}
2020
}
2121

@@ -24,15 +24,19 @@ pub fn capitalize_first(input: &str) -> String {
2424
// Return a vector of strings.
2525
// ["hello", "world"] -> ["Hello", "World"]
2626
pub fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
27-
vec![]
27+
let mut cwv = vec![];
28+
for word in words {
29+
cwv.push(capitalize_first(word));
30+
}
31+
cwv
2832
}
2933

3034
// Step 3.
3135
// Apply the `capitalize_first` function again to a slice of string slices.
3236
// Return a single string.
3337
// ["hello", " ", "world"] -> "Hello World"
3438
pub fn capitalize_words_string(words: &[&str]) -> String {
35-
String::new()
39+
capitalize_words_vector(words).join("")
3640
}
3741

3842
#[cfg(test)]

exercises/lifetimes/lifetimes1.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
// Execute `rustlings hint lifetimes1` or use the `hint` watch subcommand for a
99
// hint.
1010

11-
// I AM NOT DONE
1211

13-
fn longest(x: &str, y: &str) -> &str {
12+
13+
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
1414
if x.len() > y.len() {
1515
x
1616
} else {

exercises/lifetimes/lifetimes2.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// Execute `rustlings hint lifetimes2` or use the `hint` watch subcommand for a
77
// hint.
88

9-
// I AM NOT DONE
9+
1010

1111
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
1212
if x.len() > y.len() {
@@ -22,6 +22,6 @@ fn main() {
2222
{
2323
let string2 = String::from("xyz");
2424
result = longest(string1.as_str(), string2.as_str());
25+
println!("The longest string is '{}'", result);
2526
}
26-
println!("The longest string is '{}'", result);
2727
}

exercises/lifetimes/lifetimes3.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
// Execute `rustlings hint lifetimes3` or use the `hint` watch subcommand for a
66
// hint.
77

8-
// I AM NOT DONE
98

10-
struct Book {
11-
author: &str,
12-
title: &str,
9+
10+
struct Book<'a> {
11+
author: &'a str,
12+
title: &'a str,
1313
}
1414

1515
fn main() {

exercises/macros/macros1.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Execute `rustlings hint macros1` or use the `hint` watch subcommand for a
44
// hint.
55

6-
// I AM NOT DONE
6+
77

88
macro_rules! my_macro {
99
() => {
@@ -12,5 +12,5 @@ macro_rules! my_macro {
1212
}
1313

1414
fn main() {
15-
my_macro();
15+
my_macro!();
1616
}

exercises/macros/macros2.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
// Execute `rustlings hint macros2` or use the `hint` watch subcommand for a
44
// hint.
55

6-
// I AM NOT DONE
7-
8-
fn main() {
9-
my_macro!();
10-
}
116

127
macro_rules! my_macro {
138
() => {
149
println!("Check out my macro!");
1510
};
1611
}
12+
13+
fn main() {
14+
my_macro!();
15+
}
16+

exercises/smart_pointers/arc1.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@
2121
//
2222
// Execute `rustlings hint arc1` or use the `hint` watch subcommand for a hint.
2323

24-
// I AM NOT DONE
24+
2525

2626
#![forbid(unused_imports)] // Do not change this, (or the next) line.
2727
use std::sync::Arc;
2828
use std::thread;
2929

3030
fn main() {
3131
let numbers: Vec<_> = (0..100u32).collect();
32-
let shared_numbers = // TODO
32+
let shared_numbers = Arc::new(numbers);// TODO
3333
let mut joinhandles = Vec::new();
3434

3535
for offset in 0..8 {
36-
let child_numbers = // TODO
36+
let child_numbers = Arc::clone(&shared_numbers);// TODO
3737
joinhandles.push(thread::spawn(move || {
3838
let sum: u32 = child_numbers.iter().filter(|&&n| n % 8 == offset).sum();
3939
println!("Sum of offset {} is {}", offset, sum);

exercises/smart_pointers/box1.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
//
1919
// Execute `rustlings hint box1` or use the `hint` watch subcommand for a hint.
2020

21-
// I AM NOT DONE
21+
2222

2323
#[derive(PartialEq, Debug)]
2424
pub enum List {
25-
Cons(i32, List),
25+
Cons(i32, Box<List>),
2626
Nil,
2727
}
2828

@@ -35,11 +35,11 @@ fn main() {
3535
}
3636

3737
pub fn create_empty_list() -> List {
38-
todo!()
38+
List::Nil
3939
}
4040

4141
pub fn create_non_empty_list() -> List {
42-
todo!()
42+
List::Cons(1, Box::new(List::Nil))
4343
}
4444

4545
#[cfg(test)]

exercises/smart_pointers/cow1.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//
1313
// Execute `rustlings hint cow1` or use the `hint` watch subcommand for a hint.
1414

15-
// I AM NOT DONE
15+
1616

1717
use std::borrow::Cow;
1818

@@ -49,6 +49,8 @@ mod tests {
4949
let mut input = Cow::from(&slice[..]);
5050
match abs_all(&mut input) {
5151
// TODO
52+
Cow::Borrowed(_) => Ok(()),
53+
_ => Err("Expected owned value"),
5254
}
5355
}
5456

@@ -61,6 +63,8 @@ mod tests {
6163
let mut input = Cow::from(slice);
6264
match abs_all(&mut input) {
6365
// TODO
66+
Cow::Owned(_) => Ok(()),
67+
_ => Err("Expected owned value"),
6468
}
6569
}
6670

@@ -73,6 +77,8 @@ mod tests {
7377
let mut input = Cow::from(slice);
7478
match abs_all(&mut input) {
7579
// TODO
80+
Cow::Owned(_) => Ok(()),
81+
_ => Err("Expected owned value"),
7682
}
7783
}
7884
}

0 commit comments

Comments
 (0)