This is my first time trying Rust. I wrote this code to compute the greater common divisor of 2 integers. Any advice to improve it? Am I using modularity/reference/borrow/dereference the correct way?
File src/gcd.rs
:
// calculate the gcd of two integers
// https://en.wikipedia.org/wiki/Greatest_common_divisor
pub fn gcd(a: &i64, b: &i64) -> i64 {
let mut _a = *a;
let mut _b = *b;
while _b != 0 {
let tmp = _a;
_a = _b;
_b = tmp % _b;
}
_a
}
#[cfg(test)]
mod tests {
use ::gcd;
#[test]
fn test_gcd() {
assert_eq!(gcd::gcd(&60, &168), 12);
assert_eq!(gcd::gcd(&1071, &1029), 21);
}
}
File src/main.rs
:
mod gcd;
fn main() {
println!("gcd({}, {}) = {}", 60, 168, gcd::gcd(&60, &168));
}
Directory structure:
$ tree
.
├── Cargo.lock
├── Cargo.toml
├── src
│ ├── gcd.rs
│ └── main.rs
└── target
├── ...