pub mod fb
{
pub enum Term <T: ::std::cmp::Eq + ::std::ops::Rem + ::std::fmt::Display>
{
Num(T),
Txt(::std::borrow::Cow<'static, str>)
}
pub fn nth<T : ::std::cmp::Eq + ::std::ops::Rem + ::std::fmt::Display>(i: T) -> self::Term<T>
{
match (i % 3, i % 5)
{
(0, 0) => Term::Txt(::std::borrow::Cow::from("FizzBuzz")),
(0, _) => Term::Txt(::std::borrow::Cow::from("Fizz")),
(_, 0) => Term::Txt(::std::borrow::Cow::from("Buzz")),
_ => Term::Num(i)
}
}
}
fn main()
{
for i in 1 ..= 100
{
println!("{}", fb::nth(i));
}
}
In the implementation of my fizzbuzz, I have used raw 0
, 3
and 5
(which are {integers}
). Thus the compiler complains:
error[E0308]: mismatched types --> src/main.rs:11:20
|
11 | match (i % 3, i % 5)
| ^ expected type parameter, found integral variable
|
= note: expected type `T`
found type `{integer}`
Now how can I convince rustc that 3, 5 and 0 implements traits described by T
?