Implement countdown using generic mathematics

This commit is contained in:
Alexander Launi
2020-03-21 16:41:02 -04:00
parent 2aa26293b0
commit 2ff4f42d43
2 changed files with 9 additions and 3 deletions

View File

@@ -7,3 +7,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
num-traits = "0.2"

View File

@@ -1,10 +1,15 @@
fn countdown(i: u32) {
use std::ops::Sub;
use std::fmt::Display;
use num_traits::identities::One;
use num_traits::identities::Zero;
fn countdown<T: Display + One + Zero + Sub<Output = T>>(i: T) {
println!("{}", i);
if i == 0 {
if i.is_zero() {
return
}
countdown(i - 1);
countdown(i - T::one());
}
fn main() {