#What is wrong with my code here

15 messages · Page 1 of 1 (latest)

peak ginkgo
#
        if self.num == 0 {
            0
        }
        self.sign.as_i8()
    }```

I get the error `expected (),  found integer`

Does rust not understand that this code will always return an integer?
#

Putting an else clause seems to fix it

dim trout
#

you could also do return 0;

peak ginkgo
#

alright

#

do you know why this doesnt work?

#

does rust just not have that type of inference?

dim trout
#

what it's seeing is you want to return 0 from the if block, not the function

#

then fall through and return that last statement you have

finite path
#

Only the last expression is implicit returned. if counts as one expression with its else if it has one, but your code as written has two expressions

#

self.sign.as_i8() is implicitly returned, and the if is not

#

By using an else, you can merge them into one expression. Alternatively, you can explicitly return the 0

peak ginkgo
#

got it thanks

#

Unrelated question

#

I'm trying to match the return value of signum() which returns 1, 0 or -1 but match is exhaustive so it complains if I match only those arms. What I've done instead is this:

   match num.signum() * denom.signum() {
       1 => sign = Sign::Positive,
       -1 => sign = Sign::Negative,
       0 => sign = Sign::Positive,
       _ => sign = Sign::Positive // Eww
   }```

Which I doesn't seem like great code
#

how can I go about this in a better way