find the greatest common divisor of two numbers in rust

Here's a recursive implementation of the Euclidean algorithm to find the greatest common divisor of two numbers in Rust:

main.rs
fn gcd(mut a: i32, mut b: i32) -> i32 {
    if b == 0 {
        a
    } else {
        gcd(b, a % b)
    }
}
109 chars
8 lines

The above code defines a function named gcd that takes a and b as arguments and returns their greatest common divisor. The function checks if b is equal to zero. If yes, then it returns a. Otherwise, it applies the modulo operator to a and b and makes a recursive call with b and the result of the modulo as arguments until b is zero.

gistlibby LogSnag