Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Function that expects u16 return type errors, mismatched types even though I am using return to get values early out of function

I am getting mismatched types error, expected u16, found () from this function (code below) and I can’t seem to find why because it is guaranteed to return u16 or am I missing something?

assign_value is a method of my struct and type self.fields is Vec<Vec<u16>>.

    pub fn assign_value(&self, input: u16) -> u16 {
        for (row, vector) in self.fields.iter().enumerate() {
            for (collumn, value) in self.fields[row].iter().enumerate() {
                if *value == 0 {
                    return input;
                }else {
                    return *value;
                }
            }
        } 
    }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

What if self.fields is empty? In this case, the loop won’t be executed at all. What will be returned?

In general, all loop kinds except loop without break cannot affect whether the function will return, because they can not run.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading