0
use std::num::Int;

fn main() {
    println!("{}", add_one(4));
}

fn add_one<T: Int>(x: T) -> T {
    return x + 1
}

I'm trying to make add_one generic for Int but when I compile it says types are mismatched

Error message:

src/main.rs:8:16: 8:17 error: mismatched types:
 expected `T`,
    found `_`
(expected type parameter,
    found integral variable) [E0308]
src/main.rs:8     return x + 1
                             ^
2bdkid
  • 91
  • 1
  • 1
  • 6
  • Please *include* the specific error in your question. You should also add what part of the error you don't understand. – Shepmaster Mar 23 '15 at 15:58
  • 2
    possible duplicate of [Cannot create a generic function that uses a literal zero](http://stackoverflow.com/questions/27952003/cannot-create-a-generic-function-that-uses-a-literal-zero) – Shepmaster Mar 23 '15 at 15:59

1 Answers1

3

I looked into std::num::Int a bit more and found Int::one().

This works for now:

use std::num::Int;

fn main() {
    println!("{}", add_one(4));
}

fn add_one<T: Int>(x: T) -> T {
    return x + Int::one();
}
2bdkid
  • 91
  • 1
  • 1
  • 6