You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm new to Rust and working through the iterators3 exercise. I encountered some ambiguity regarding the expected return types for the result_with_list and list_of_results functions.
Based on the input (let numbers = [27, 297, 38502, 81];) being an array and the format of the desired output comments (// Desired output: Ok([1, 11, 1426, 3]) and // Desired output: [Ok(1), Ok(11), Ok(1426), Ok(3)]), I interpreted the task as requiring the functions to return fixed-size arrays rather than dynamically sized Vectors.
I came up with the following solutions which pass the tests by returning arrays:
fndivide(a:i64,b:i64) -> Result<i64,DivisionError>{/* implemented as in official solution*/}// TODO: Add the correct return type and complete the function body.// Desired output: `Ok([1, 11, 1426, 3])`fnresult_with_list() -> Result<[i64;4],DivisionError>{let numbers = [27,297,38502,81];letmut division_results = numbers.into_iter().map(|n| divide(n,27));Ok([
division_results.next().unwrap()?,
division_results.next().unwrap()?,
division_results.next().unwrap()?,
division_results.next().unwrap()?,])}// TODO: Add the correct return type and complete the function body.// Desired output: `[Ok(1), Ok(11), Ok(1426), Ok(3)]`fnlist_of_results() -> [Result<i64,DivisionError>;4]{let numbers = [27,297,38502,81];letmut division_results = numbers.into_iter().map(|n| divide(n,27));[
division_results.next().unwrap(),
division_results.next().unwrap(),
division_results.next().unwrap(),
division_results.next().unwrap(),]}
While these solutions work, they feel somewhat "inelegant" because they require manually calling next().unwrap() multiple times, rather than using a more idiomatic iterator adapter like collect(). I understand that collect() typically targets collections like Vec, and collecting directly into an array [T; N] (especially Result<[T; N], E> from an iterator of Result<T, E>) isn't straightforward using stable Rust features alone.
My Questions:
Is the interpretation that the functions should return fixed-size arrays (Result<[i64; 4], _> and [Result<i64, DivisionError>; 4]) consistent with the intended design of the exercise?
If returning arrays is intended, is there a more idiomatic or elegant way to achieve this collection from the iterator within the typical scope of Rustlings exercises (e.g., without nightly features or external crates)?
If the intended solution involves returning Vec (e.g., Result<Vec<i64>, _> and Vec<Result<i64, _>>), would it be helpful to clarify this expectation in the exercise comments or description? This could prevent confusion for learners who might focus on the array-like syntax in the "Desired output" comments.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm new to Rust and working through the iterators3 exercise. I encountered some ambiguity regarding the expected return types for the
result_with_list
andlist_of_results
functions.Based on the input (
let numbers = [27, 297, 38502, 81];
) being an array and the format of the desired output comments (// Desired output: Ok([1, 11, 1426, 3]
) and// Desired output: [Ok(1), Ok(11), Ok(1426), Ok(3)])
, I interpreted the task as requiring the functions to return fixed-size arrays rather than dynamically sized Vectors.I came up with the following solutions which pass the tests by returning arrays:
While these solutions work, they feel somewhat "inelegant" because they require manually calling
next().unwrap()
multiple times, rather than using a more idiomatic iterator adapter likecollect()
. I understand thatcollect()
typically targets collections likeVec
, and collecting directly into an array[T; N]
(especiallyResult<[T; N], E>
from an iterator ofResult<T, E>
) isn't straightforward using stable Rust features alone.My Questions:
Is the interpretation that the functions should return fixed-size arrays (
Result<[i64; 4], _>
and[Result<i64, DivisionError>; 4]
) consistent with the intended design of the exercise?If returning arrays is intended, is there a more idiomatic or elegant way to achieve this collection from the iterator within the typical scope of Rustlings exercises (e.g., without nightly features or external crates)?
If the intended solution involves returning
Vec
(e.g.,Result<Vec<i64>, _>
andVec<Result<i64, _>>
), would it be helpful to clarify this expectation in the exercise comments or description? This could prevent confusion for learners who might focus on the array-like syntax in the "Desired output" comments.Beta Was this translation helpful? Give feedback.
All reactions