Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Why does this work? .collect() automatic conversion to function return type

+5
−0

I'm completing the rustlings exercises as part of self-teaching Rust. While working on the third iterators exercise, I solved the exercise but don't quite understand why my solution works.

Specifically, I made two functions that are identical except for their name and return type, but the function body is the same in each:

// Complete the function and return a value of the correct type so the test
// passes.
// Desired output: Ok([1, 11, 1426, 3])
fn result_with_list() -> Result<Vec<i32>, DivisionError> {
    let numbers = vec![27, 297, 38502, 81];
    numbers.into_iter().map(|n| divide(n, 27)).collect()
}

// Complete the function and return a value of the correct type so the test
// passes.
// Desired output: [Ok(1), Ok(11), Ok(1426), Ok(3)]
fn list_of_results() -> Vec<Result<i32, DivisionError>> {
    let numbers = vec![27, 297, 38502, 81];
    numbers.into_iter().map(|n| divide(n, 27)).collect()
}

Some sort of automatic conversion must be occuring, likely due to Iterator::collect() having implemented the std::iter::FromIterator trait. Is that right or am I on the wrong track? How would I make the final type more explicit even though I don't need to, so that I can see how to do so (likely via some turbofish notation)?

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

1 answer

+3
−0

You're very much on the right track.

std::iter::FromIterator trait

This trait is indeed what makes it work. In particular, both functions' return types implement the trait:

std::result::Result<A,E> implements it if A implements it.[1] And here A = std::Vec.

std::Vec implements it as long as the iterated type matches the type contained in the Vec.[2]

Turbofish operator

Yes you can force collect's output type with the turbofish operator:

someIter.collect::<Vec<Result<i32, DivisionError>>()

Or you don't have to specify the whole type:

someIter.collect::<Vec<Result<_>>()
someIter.collect::<Vec<_>()

If not on the returning line, you could also use type annotations

let vec_of_results: <Vec<Result<i32, DivisionError>> = someIter.collect();

  1. Documentation: https://doc.rust-lang.org/std/result/enum.Result.html#impl-FromIterator<Result<A,+E>>-for-Result<V,+E> ↩︎

  2. Documentation: https://doc.rust-lang.org/std/vec/struct.Vec.html#impl-FromIterator<T>-for-Vec<T> ↩︎

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »