Skip to content

Commit 6b31782

Browse files
committed
changing solutions/iterators/iterators3.rs
update the result_with_list function and list_of_results function to taking parameters
1 parent e73fff3 commit 6b31782

File tree

2 files changed

+246
-10
lines changed

2 files changed

+246
-10
lines changed

dev-Cargo.toml

Lines changed: 223 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,223 @@
1-
dev/Cargo.toml
1+
# Don't edit the `bin` list manually! It is updated by `cargo dev update`. This comment line will be stripped in `rustlings init`.
2+
bin = [
3+
{ name = "intro1", path = "../exercises/00_intro/intro1.rs" },
4+
{ name = "intro1_sol", path = "../solutions/00_intro/intro1.rs" },
5+
{ name = "intro2", path = "../exercises/00_intro/intro2.rs" },
6+
{ name = "intro2_sol", path = "../solutions/00_intro/intro2.rs" },
7+
{ name = "variables1", path = "../exercises/01_variables/variables1.rs" },
8+
{ name = "variables1_sol", path = "../solutions/01_variables/variables1.rs" },
9+
{ name = "variables2", path = "../exercises/01_variables/variables2.rs" },
10+
{ name = "variables2_sol", path = "../solutions/01_variables/variables2.rs" },
11+
{ name = "variables3", path = "../exercises/01_variables/variables3.rs" },
12+
{ name = "variables3_sol", path = "../solutions/01_variables/variables3.rs" },
13+
{ name = "variables4", path = "../exercises/01_variables/variables4.rs" },
14+
{ name = "variables4_sol", path = "../solutions/01_variables/variables4.rs" },
15+
{ name = "variables5", path = "../exercises/01_variables/variables5.rs" },
16+
{ name = "variables5_sol", path = "../solutions/01_variables/variables5.rs" },
17+
{ name = "variables6", path = "../exercises/01_variables/variables6.rs" },
18+
{ name = "variables6_sol", path = "../solutions/01_variables/variables6.rs" },
19+
{ name = "functions1", path = "../exercises/02_functions/functions1.rs" },
20+
{ name = "functions1_sol", path = "../solutions/02_functions/functions1.rs" },
21+
{ name = "functions2", path = "../exercises/02_functions/functions2.rs" },
22+
{ name = "functions2_sol", path = "../solutions/02_functions/functions2.rs" },
23+
{ name = "functions3", path = "../exercises/02_functions/functions3.rs" },
24+
{ name = "functions3_sol", path = "../solutions/02_functions/functions3.rs" },
25+
{ name = "functions4", path = "../exercises/02_functions/functions4.rs" },
26+
{ name = "functions4_sol", path = "../solutions/02_functions/functions4.rs" },
27+
{ name = "functions5", path = "../exercises/02_functions/functions5.rs" },
28+
{ name = "functions5_sol", path = "../solutions/02_functions/functions5.rs" },
29+
{ name = "if1", path = "../exercises/03_if/if1.rs" },
30+
{ name = "if1_sol", path = "../solutions/03_if/if1.rs" },
31+
{ name = "if2", path = "../exercises/03_if/if2.rs" },
32+
{ name = "if2_sol", path = "../solutions/03_if/if2.rs" },
33+
{ name = "if3", path = "../exercises/03_if/if3.rs" },
34+
{ name = "if3_sol", path = "../solutions/03_if/if3.rs" },
35+
{ name = "quiz1", path = "../exercises/quizzes/quiz1.rs" },
36+
{ name = "quiz1_sol", path = "../solutions/quizzes/quiz1.rs" },
37+
{ name = "primitive_types1", path = "../exercises/04_primitive_types/primitive_types1.rs" },
38+
{ name = "primitive_types1_sol", path = "../solutions/04_primitive_types/primitive_types1.rs" },
39+
{ name = "primitive_types2", path = "../exercises/04_primitive_types/primitive_types2.rs" },
40+
{ name = "primitive_types2_sol", path = "../solutions/04_primitive_types/primitive_types2.rs" },
41+
{ name = "primitive_types3", path = "../exercises/04_primitive_types/primitive_types3.rs" },
42+
{ name = "primitive_types3_sol", path = "../solutions/04_primitive_types/primitive_types3.rs" },
43+
{ name = "primitive_types4", path = "../exercises/04_primitive_types/primitive_types4.rs" },
44+
{ name = "primitive_types4_sol", path = "../solutions/04_primitive_types/primitive_types4.rs" },
45+
{ name = "primitive_types5", path = "../exercises/04_primitive_types/primitive_types5.rs" },
46+
{ name = "primitive_types5_sol", path = "../solutions/04_primitive_types/primitive_types5.rs" },
47+
{ name = "primitive_types6", path = "../exercises/04_primitive_types/primitive_types6.rs" },
48+
{ name = "primitive_types6_sol", path = "../solutions/04_primitive_types/primitive_types6.rs" },
49+
{ name = "vecs1", path = "../exercises/05_vecs/vecs1.rs" },
50+
{ name = "vecs1_sol", path = "../solutions/05_vecs/vecs1.rs" },
51+
{ name = "vecs2", path = "../exercises/05_vecs/vecs2.rs" },
52+
{ name = "vecs2_sol", path = "../solutions/05_vecs/vecs2.rs" },
53+
{ name = "move_semantics1", path = "../exercises/06_move_semantics/move_semantics1.rs" },
54+
{ name = "move_semantics1_sol", path = "../solutions/06_move_semantics/move_semantics1.rs" },
55+
{ name = "move_semantics2", path = "../exercises/06_move_semantics/move_semantics2.rs" },
56+
{ name = "move_semantics2_sol", path = "../solutions/06_move_semantics/move_semantics2.rs" },
57+
{ name = "move_semantics3", path = "../exercises/06_move_semantics/move_semantics3.rs" },
58+
{ name = "move_semantics3_sol", path = "../solutions/06_move_semantics/move_semantics3.rs" },
59+
{ name = "move_semantics4", path = "../exercises/06_move_semantics/move_semantics4.rs" },
60+
{ name = "move_semantics4_sol", path = "../solutions/06_move_semantics/move_semantics4.rs" },
61+
{ name = "move_semantics5", path = "../exercises/06_move_semantics/move_semantics5.rs" },
62+
{ name = "move_semantics5_sol", path = "../solutions/06_move_semantics/move_semantics5.rs" },
63+
{ name = "structs1", path = "../exercises/07_structs/structs1.rs" },
64+
{ name = "structs1_sol", path = "../solutions/07_structs/structs1.rs" },
65+
{ name = "structs2", path = "../exercises/07_structs/structs2.rs" },
66+
{ name = "structs2_sol", path = "../solutions/07_structs/structs2.rs" },
67+
{ name = "structs3", path = "../exercises/07_structs/structs3.rs" },
68+
{ name = "structs3_sol", path = "../solutions/07_structs/structs3.rs" },
69+
{ name = "enums1", path = "../exercises/08_enums/enums1.rs" },
70+
{ name = "enums1_sol", path = "../solutions/08_enums/enums1.rs" },
71+
{ name = "enums2", path = "../exercises/08_enums/enums2.rs" },
72+
{ name = "enums2_sol", path = "../solutions/08_enums/enums2.rs" },
73+
{ name = "enums3", path = "../exercises/08_enums/enums3.rs" },
74+
{ name = "enums3_sol", path = "../solutions/08_enums/enums3.rs" },
75+
{ name = "strings1", path = "../exercises/09_strings/strings1.rs" },
76+
{ name = "strings1_sol", path = "../solutions/09_strings/strings1.rs" },
77+
{ name = "strings2", path = "../exercises/09_strings/strings2.rs" },
78+
{ name = "strings2_sol", path = "../solutions/09_strings/strings2.rs" },
79+
{ name = "strings3", path = "../exercises/09_strings/strings3.rs" },
80+
{ name = "strings3_sol", path = "../solutions/09_strings/strings3.rs" },
81+
{ name = "strings4", path = "../exercises/09_strings/strings4.rs" },
82+
{ name = "strings4_sol", path = "../solutions/09_strings/strings4.rs" },
83+
{ name = "modules1", path = "../exercises/10_modules/modules1.rs" },
84+
{ name = "modules1_sol", path = "../solutions/10_modules/modules1.rs" },
85+
{ name = "modules2", path = "../exercises/10_modules/modules2.rs" },
86+
{ name = "modules2_sol", path = "../solutions/10_modules/modules2.rs" },
87+
{ name = "modules3", path = "../exercises/10_modules/modules3.rs" },
88+
{ name = "modules3_sol", path = "../solutions/10_modules/modules3.rs" },
89+
{ name = "hashmaps1", path = "../exercises/11_hashmaps/hashmaps1.rs" },
90+
{ name = "hashmaps1_sol", path = "../solutions/11_hashmaps/hashmaps1.rs" },
91+
{ name = "hashmaps2", path = "../exercises/11_hashmaps/hashmaps2.rs" },
92+
{ name = "hashmaps2_sol", path = "../solutions/11_hashmaps/hashmaps2.rs" },
93+
{ name = "hashmaps3", path = "../exercises/11_hashmaps/hashmaps3.rs" },
94+
{ name = "hashmaps3_sol", path = "../solutions/11_hashmaps/hashmaps3.rs" },
95+
{ name = "quiz2", path = "../exercises/quizzes/quiz2.rs" },
96+
{ name = "quiz2_sol", path = "../solutions/quizzes/quiz2.rs" },
97+
{ name = "options1", path = "../exercises/12_options/options1.rs" },
98+
{ name = "options1_sol", path = "../solutions/12_options/options1.rs" },
99+
{ name = "options2", path = "../exercises/12_options/options2.rs" },
100+
{ name = "options2_sol", path = "../solutions/12_options/options2.rs" },
101+
{ name = "options3", path = "../exercises/12_options/options3.rs" },
102+
{ name = "options3_sol", path = "../solutions/12_options/options3.rs" },
103+
{ name = "errors1", path = "../exercises/13_error_handling/errors1.rs" },
104+
{ name = "errors1_sol", path = "../solutions/13_error_handling/errors1.rs" },
105+
{ name = "errors2", path = "../exercises/13_error_handling/errors2.rs" },
106+
{ name = "errors2_sol", path = "../solutions/13_error_handling/errors2.rs" },
107+
{ name = "errors3", path = "../exercises/13_error_handling/errors3.rs" },
108+
{ name = "errors3_sol", path = "../solutions/13_error_handling/errors3.rs" },
109+
{ name = "errors4", path = "../exercises/13_error_handling/errors4.rs" },
110+
{ name = "errors4_sol", path = "../solutions/13_error_handling/errors4.rs" },
111+
{ name = "errors5", path = "../exercises/13_error_handling/errors5.rs" },
112+
{ name = "errors5_sol", path = "../solutions/13_error_handling/errors5.rs" },
113+
{ name = "errors6", path = "../exercises/13_error_handling/errors6.rs" },
114+
{ name = "errors6_sol", path = "../solutions/13_error_handling/errors6.rs" },
115+
{ name = "generics1", path = "../exercises/14_generics/generics1.rs" },
116+
{ name = "generics1_sol", path = "../solutions/14_generics/generics1.rs" },
117+
{ name = "generics2", path = "../exercises/14_generics/generics2.rs" },
118+
{ name = "generics2_sol", path = "../solutions/14_generics/generics2.rs" },
119+
{ name = "traits1", path = "../exercises/15_traits/traits1.rs" },
120+
{ name = "traits1_sol", path = "../solutions/15_traits/traits1.rs" },
121+
{ name = "traits2", path = "../exercises/15_traits/traits2.rs" },
122+
{ name = "traits2_sol", path = "../solutions/15_traits/traits2.rs" },
123+
{ name = "traits3", path = "../exercises/15_traits/traits3.rs" },
124+
{ name = "traits3_sol", path = "../solutions/15_traits/traits3.rs" },
125+
{ name = "traits4", path = "../exercises/15_traits/traits4.rs" },
126+
{ name = "traits4_sol", path = "../solutions/15_traits/traits4.rs" },
127+
{ name = "traits5", path = "../exercises/15_traits/traits5.rs" },
128+
{ name = "traits5_sol", path = "../solutions/15_traits/traits5.rs" },
129+
{ name = "quiz3", path = "../exercises/quizzes/quiz3.rs" },
130+
{ name = "quiz3_sol", path = "../solutions/quizzes/quiz3.rs" },
131+
{ name = "lifetimes1", path = "../exercises/16_lifetimes/lifetimes1.rs" },
132+
{ name = "lifetimes1_sol", path = "../solutions/16_lifetimes/lifetimes1.rs" },
133+
{ name = "lifetimes2", path = "../exercises/16_lifetimes/lifetimes2.rs" },
134+
{ name = "lifetimes2_sol", path = "../solutions/16_lifetimes/lifetimes2.rs" },
135+
{ name = "lifetimes3", path = "../exercises/16_lifetimes/lifetimes3.rs" },
136+
{ name = "lifetimes3_sol", path = "../solutions/16_lifetimes/lifetimes3.rs" },
137+
{ name = "tests1", path = "../exercises/17_tests/tests1.rs" },
138+
{ name = "tests1_sol", path = "../solutions/17_tests/tests1.rs" },
139+
{ name = "tests2", path = "../exercises/17_tests/tests2.rs" },
140+
{ name = "tests2_sol", path = "../solutions/17_tests/tests2.rs" },
141+
{ name = "tests3", path = "../exercises/17_tests/tests3.rs" },
142+
{ name = "tests3_sol", path = "../solutions/17_tests/tests3.rs" },
143+
{ name = "iterators1", path = "../exercises/18_iterators/iterators1.rs" },
144+
{ name = "iterators1_sol", path = "../solutions/18_iterators/iterators1.rs" },
145+
{ name = "iterators2", path = "../exercises/18_iterators/iterators2.rs" },
146+
{ name = "iterators2_sol", path = "../solutions/18_iterators/iterators2.rs" },
147+
{ name = "iterators3", path = "../exercises/18_iterators/iterators3.rs" },
148+
{ name = "iterators3_sol", path = "../solutions/18_iterators/iterators3.rs" },
149+
{ name = "iterators4", path = "../exercises/18_iterators/iterators4.rs" },
150+
{ name = "iterators4_sol", path = "../solutions/18_iterators/iterators4.rs" },
151+
{ name = "iterators5", path = "../exercises/18_iterators/iterators5.rs" },
152+
{ name = "iterators5_sol", path = "../solutions/18_iterators/iterators5.rs" },
153+
{ name = "box1", path = "../exercises/19_smart_pointers/box1.rs" },
154+
{ name = "box1_sol", path = "../solutions/19_smart_pointers/box1.rs" },
155+
{ name = "rc1", path = "../exercises/19_smart_pointers/rc1.rs" },
156+
{ name = "rc1_sol", path = "../solutions/19_smart_pointers/rc1.rs" },
157+
{ name = "arc1", path = "../exercises/19_smart_pointers/arc1.rs" },
158+
{ name = "arc1_sol", path = "../solutions/19_smart_pointers/arc1.rs" },
159+
{ name = "cow1", path = "../exercises/19_smart_pointers/cow1.rs" },
160+
{ name = "cow1_sol", path = "../solutions/19_smart_pointers/cow1.rs" },
161+
{ name = "threads1", path = "../exercises/20_threads/threads1.rs" },
162+
{ name = "threads1_sol", path = "../solutions/20_threads/threads1.rs" },
163+
{ name = "threads2", path = "../exercises/20_threads/threads2.rs" },
164+
{ name = "threads2_sol", path = "../solutions/20_threads/threads2.rs" },
165+
{ name = "threads3", path = "../exercises/20_threads/threads3.rs" },
166+
{ name = "threads3_sol", path = "../solutions/20_threads/threads3.rs" },
167+
{ name = "macros1", path = "../exercises/21_macros/macros1.rs" },
168+
{ name = "macros1_sol", path = "../solutions/21_macros/macros1.rs" },
169+
{ name = "macros2", path = "../exercises/21_macros/macros2.rs" },
170+
{ name = "macros2_sol", path = "../solutions/21_macros/macros2.rs" },
171+
{ name = "macros3", path = "../exercises/21_macros/macros3.rs" },
172+
{ name = "macros3_sol", path = "../solutions/21_macros/macros3.rs" },
173+
{ name = "macros4", path = "../exercises/21_macros/macros4.rs" },
174+
{ name = "macros4_sol", path = "../solutions/21_macros/macros4.rs" },
175+
{ name = "clippy1", path = "../exercises/22_clippy/clippy1.rs" },
176+
{ name = "clippy1_sol", path = "../solutions/22_clippy/clippy1.rs" },
177+
{ name = "clippy2", path = "../exercises/22_clippy/clippy2.rs" },
178+
{ name = "clippy2_sol", path = "../solutions/22_clippy/clippy2.rs" },
179+
{ name = "clippy3", path = "../exercises/22_clippy/clippy3.rs" },
180+
{ name = "clippy3_sol", path = "../solutions/22_clippy/clippy3.rs" },
181+
{ name = "using_as", path = "../exercises/23_conversions/using_as.rs" },
182+
{ name = "using_as_sol", path = "../solutions/23_conversions/using_as.rs" },
183+
{ name = "from_into", path = "../exercises/23_conversions/from_into.rs" },
184+
{ name = "from_into_sol", path = "../solutions/23_conversions/from_into.rs" },
185+
{ name = "from_str", path = "../exercises/23_conversions/from_str.rs" },
186+
{ name = "from_str_sol", path = "../solutions/23_conversions/from_str.rs" },
187+
{ name = "try_from_into", path = "../exercises/23_conversions/try_from_into.rs" },
188+
{ name = "try_from_into_sol", path = "../solutions/23_conversions/try_from_into.rs" },
189+
{ name = "as_ref_mut", path = "../exercises/23_conversions/as_ref_mut.rs" },
190+
{ name = "as_ref_mut_sol", path = "../solutions/23_conversions/as_ref_mut.rs" },
191+
]
192+
193+
[package]
194+
name = "exercises"
195+
edition = "2024"
196+
# Don't publish the exercises on crates.io!
197+
publish = false
198+
199+
[profile.release]
200+
panic = "abort"
201+
202+
[profile.dev]
203+
panic = "abort"
204+
205+
[lints.rust]
206+
# You shouldn't write unsafe code in Rustlings!
207+
unsafe_code = "forbid"
208+
# You don't need unstable features in Rustlings and shouldn't rely on them while learning Rust.
209+
unstable_features = "forbid"
210+
# Dead code warnings can't be avoided in some exercises and might distract while learning.
211+
dead_code = "allow"
212+
213+
[lints.clippy]
214+
# You forgot a `todo!()`!
215+
todo = "forbid"
216+
# This can only happen by mistake in Rustlings.
217+
empty_loop = "forbid"
218+
# No infinite loops are needed in Rustlings.
219+
infinite_loop = "deny"
220+
# You shouldn't leak memory while still learning Rust!
221+
mem_forget = "deny"
222+
# Currently, there are no disallowed methods. This line avoids problems when developing Rustlings.
223+
disallowed_methods = "allow"

solutions/18_iterators/iterators3.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,33 @@ fn divide(a: i64, b: i64) -> Result<i64, DivisionError> {
2424
Ok(a / b)
2525
}
2626

27-
fn result_with_list() -> Result<Vec<i64>, DivisionError> {
27+
fn result_with_list(numbers:Vec<i64>,b:i64) -> Result<Vec<i64>, DivisionError> {
2828
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
29-
let numbers = [27, 297, 38502, 81];
30-
let division_results = numbers.into_iter().map(|n| divide(n, 27));
29+
let division_results = numbers.into_iter().map(|n| divide(n,b));
3130
// Collects to the expected return type. Returns the first error in the
3231
// division results (if one exists).
33-
division_results.collect()
32+
match division_results.find(|n|{
33+
match n {
34+
Ok(num)=> return false,
35+
Err(e)=> return true
36+
}
37+
}) {
38+
Some(r)=> return Err(r.unwrap_err()),
39+
None=>{
40+
let ans:Vec<i64>=division_results.map(|n|{
41+
return match n {
42+
Ok(num)=> return num,
43+
Err(e)=> return -1
44+
}
45+
}).collect();
46+
return Ok(ans);
47+
}
48+
}
3449
}
3550

36-
fn list_of_results() -> Vec<Result<i64, DivisionError>> {
51+
fn list_of_results(numbers:Vec<i64>,b:i64) -> Vec<Result<i64, DivisionError>> {
3752
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
38-
let numbers = [27, 297, 38502, 81];
39-
let division_results = numbers.into_iter().map(|n| divide(n, 27));
53+
let division_results = numbers.into_iter().map(|n| divide(n, b));
4054
// Collects to the expected return type.
4155
division_results.collect()
4256
}
@@ -76,11 +90,11 @@ mod tests {
7690

7791
#[test]
7892
fn test_result_with_list() {
79-
assert_eq!(result_with_list().unwrap(), [1, 11, 1426, 3]);
93+
assert_eq!(result_with_list(vec![27, 297, 38502, 81],27), [1, 11, 1426, 3]);
8094
}
8195

8296
#[test]
8397
fn test_list_of_results() {
84-
assert_eq!(list_of_results(), [Ok(1), Ok(11), Ok(1426), Ok(3)]);
98+
assert_eq!(list_of_results(vec![27, 297, 38502, 81],27), [Ok(1), Ok(11), Ok(1426), Ok(3)]);
8599
}
86100
}

0 commit comments

Comments
 (0)