Skip to content

Returning a for loop gives confusing error message if you aren't aware that can be done #18606

@mdinger

Description

@mdinger

Here the compiler complains about the for loop but the problem is that no value is being returned to the function call. This error is confusing.

fn read() -> Vec<String> {
    let data: Vec<String> = Vec::new();
    for _ in range(0i, 7i) {}
    //data
}

fn main() {
    read();
}

Returns:

<anon>:3:5: 5:2 error: mismatched types: expected `collections::vec::Vec<collections::string::String>`, found `()` (expected struct collections::vec::Vec, found ())
<anon>:3     for _ in range(0i, 7i) {}
<anon>:4     //data
<anon>:5 }
error: aborting due to previous error
playpen: application terminated with error code 101

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions