Expected type `std::vec::Vec` found type in for line

Why is the compiler expecting a Vecin the line "for name in ..." because of the return type? And how to fix that. Of course I want to fill the Vec inside the for statement.

fn regkeyloop(regpath: &str) -> Vec<Software>{
    let mut myvec = Vec::<Software>::new();
    let subkey = winreg::RegKey::predef(HKEY_LOCAL_MACHINE)
        .open_subkey_with_flags(regpath, KEY_READ)
        .unwrap();
    for name in subkey.enum_keys().map(|x| x.unwrap()) {
        myvec.push(Software {
            DisplayName : name.to_string(),
            Publisher : "Ms".to_string(),
            DisplayVersion : "1".to_string(),
        });
    return myvec;
    }
}

I IDIOT

Some kind of missmatch with } and )

This one works

fn regkeyloop(regpath: &str) -> Vec<Software>{
let mut myvec = Vec::<Software>::new();

let subkey = winreg::RegKey::predef(HKEY_LOCAL_MACHINE)
    .open_subkey_with_flags(regpath, KEY_READ)
    .unwrap();
for name in subkey.enum_keys().map(|x| x.unwrap()) {
    myvec.push(Software {
        DisplayName : name.to_string(),
        Publisher : "Ms".to_string(),
        DisplayVersion : "1".to_string(),
        })
    }
return myvec;
}

Don't forget about rustfmt - here is the formatted version, and mismatch seems to be much more clear there.

@Cerberuser As someone not familiar with this issue, I can try to run it and look at the compile error, but no from just inspecting the code I see no obvious ) vs } mismatch.
In fact I see only balanced parens and brackets, so why the error occurs at all is still a mystery to me (note that I'm not trying very hard atm).
Then again, I also don't see it in the OP so take this post for what you will :slight_smile:

I mean, when the code is properly indented, you'll see that return is inside for, not outside it.

1 Like

Ah yes, that part becomes really obvious then.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.