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.

Post History

77%
+5 −0
Q&A Why does this work? .collect() automatic conversion to function return type

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. Specifi...

1 answer  ·  posted 10mo ago by qohelet‭  ·  last activity 10mo ago by Iizuki‭

Question rust
#2: Nominated for promotion by user avatar Alexei‭ · 2024-02-21T08:09:47Z (10 months ago)
#1: Initial revision by user avatar qohelet‭ · 2024-02-20T18:54:23Z (10 months ago)
Why does this work? .collect() automatic conversion to function return type
I'm completing [the `rustlings` exercises](https://rustlings.cool/) as part of self-teaching Rust. While working on [the third iterators exercise](https://github.com/rust-lang/rustlings/blob/main/exercises/18_iterators/iterators3.rs), 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()`](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.collect) having implemented the [`std::iter::FromIterator` trait](https://doc.rust-lang.org/std/iter/trait.FromIterator.html). 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)?