Skip to content

Commit 1570c32

Browse files
committed
Auto merge of #186 - Jesse-Cameron:iterator-exercise2, r=komaeda
feat(iterators2): adds iterators2 exercise including config Hi there! I really enjoyed doing the rustlings exercises so I thought that I would try to add an exercise! This one just covers a couple of basic iterator operations. Getting people used to the `map` and `collect` functions. However, it does feel kinda similar to the next exercise. So I may also revisit some of the tests in iterators3 if we think that is necessary.
2 parents 5e1d7c3 + 9288fcc commit 1570c32

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
// iterators2.rs
2+
// In this module, you'll learn some of unique advantages that iterators can offer
3+
// Step 1. Complete the `capitalize_first` function to pass the first two cases
4+
// Step 2. Apply the `capitalize_first` function to a vector of strings, ensuring that it
5+
// Step 3. Apply the `capitalize_first` function again to a list, but try and ensure it returns a single string
6+
// As always, there are hints below!
7+
8+
pub fn capitalize_first(input: &str) -> String {
9+
let mut c = input.chars();
10+
match c.next() {
11+
None => String::new(),
12+
Some(first) => first.collect()::<String>() + c.as_str(),
13+
}
14+
}
15+
16+
#[cfg(test)]
17+
mod tests {
18+
use super::*;
19+
20+
// Step 1.
21+
// Tests that verify your `capitalize_first` function implementation
22+
#[test]
23+
fn test_success() {
24+
assert_eq!(capitalize_first("hello"), "Hello");
25+
}
26+
27+
#[test]
28+
fn test_empty() {
29+
assert_eq!(capitalize_first(""), "");
30+
}
31+
32+
// Step 2.
33+
#[test]
34+
fn test_iterate_string_vec() {
35+
let words = vec!["hello", "world"];
36+
let capitalized_words: Vec<String> = // TODO
37+
assert_eq!(capitalized_words, ["Hello", "World"]);
38+
}
39+
40+
#[test]
41+
fn test_iterate_into_string() {
42+
let words = vec!["hello", " ", "world"];
43+
let capitalized_words = // TODO
44+
assert_eq!(capitalized_words, "Hello World");
45+
}
46+
}
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+
59+
60+
61+
62+
63+
64+
65+
66+
67+
68+
69+
70+
71+
72+
73+
74+
75+
76+
77+
78+
79+
// Step 1
80+
// You need to call something on `first` before it can be collected
81+
// Currently it's type is `char`. Have a look at the methods that are available on that type:
82+
// https://doc.rust-lang.org/std/primitive.char.html
83+
84+
85+
86+
87+
88+
89+
90+
91+
92+
93+
94+
95+
96+
97+
98+
99+
100+
101+
102+
103+
104+
105+
106+
107+
108+
109+
// Step 2
110+
// First you'll need to turn the Vec into an iterator
111+
// Then you'll need to apply your function unto each item in the vector
112+
// P.s. Don't forget to collect() at the end!
113+
114+
115+
116+
117+
118+
119+
120+
121+
122+
123+
124+
125+
126+
127+
128+
129+
130+
131+
132+
133+
134+
135+
136+
137+
138+
139+
140+
141+
142+
// Step 3.
143+
// This is very similar to the previous test. The only real change is that you will need to
144+
// alter the type that collect is coerced into. For a bonus you could try doing this with a
145+
// turbofish

info.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ mode = "compile"
210210
path = "exercises/standard_library_types/arc1.rs"
211211
mode = "compile"
212212

213+
[[exercises]]
214+
path = "exercises/standard_library_types/iterators2.rs"
215+
mode = "test"
216+
213217
[[exercises]]
214218
path = "exercises/standard_library_types/iterators3.rs"
215219
mode = "test"

0 commit comments

Comments
 (0)