Greetings!
To slowly increase my understanding of Rust, I attempted to write a very simple version of the Fibonacci function (This is the most naïve, simple implementation; it is possible to transform it in an iterative solution but that distracts from its simplicity and therefore from the question):
fn fib(n: u32) > u32 {
match n {
0 => 1,
1 => 1,
_ => fib(n  1) + fib(n  2),
}
}
(Try it at the Rust Playground )
Now, I want to change this function to work with any (positive) numeric types, and not only u32
s. However, transforming fib
in a generic version has me stumped. This is what I came up with so far:
use std::ops::{Add, Sub};
fn fib<T: Add + Sub + Ord>(n: T) > T {
match n {
0 => 1,
1 => 1,
_ => fib(n  1) + fib(n  2),
}
}
(Try it on the Rust Playground)
The compiler tells me that something is missing. If I’m understanding its helpful error messages properly, the problems are:

expected type parameter, found integral variable
: I need to specify how to transform a literal integer into a value of typeT
. Is there a trait that does that?  Likewise,
the trait ```std::ops::Add
is not implemented for<T as std::ops::Sub>::Output```` seems to indicate that
n  1and
n  2cannot be turned back into a value of type
T`.  It also seems the case that because
std::ops::Add
andstd::ops::Sub
are not always isomorphic in their types (i.e.SystemTime = SystemTime  Duration
, here the RHS and LHS of the subtraction operator are different), that this needs to be enforced somehow.
How can this be done? Am I even on the right track with my approach?