This commit is contained in:
nora 2021-11-07 21:15:13 +01:00
parent b10ad7decc
commit c2ce5dcae0
2 changed files with 45 additions and 1 deletions

42
src/count.rs Normal file
View file

@ -0,0 +1,42 @@
use std::marker::PhantomData;
trait Nat {
fn int() -> i32;
}
struct Z;
impl Nat for Z {
fn int() -> i32 {
0
}
}
struct S<N: Nat>(PhantomData<N>);
impl<N: Nat> Nat for S<N> {
fn int() -> i32 {
N::int() + 1
}
}
trait Count: Nat {
fn count() -> String;
}
impl Count for Z {
fn count() -> String {
"0".to_string()
}
}
impl<N: Count> Count for S<N> {
fn count() -> String {
format!("{} {}", N::count(), Self::int())
}
}
pub fn count() {
println!("{}", <S<S<S<Z>>> as Count>::count());
}

View file

@ -1,3 +1,5 @@
mod count;
fn main() {
println!("Hello, world!");
count::count();
}