Below is a generic type Foo
. How can I correctly implement the addOne
method:
struct Foo<T> {
n: T,
}
impl<T> Foo<T> {
fn addOne(self) -> T {
self.n + 1
}
}
fn main() {
let a = Foo { n: 5 };
println!("{}", a.addOne());
}
I expect the output of 6, but this code does not compile:
error[E0369]: binary operation `+` cannot be applied to type `T`
--> src/main.rs:7:16
|
7 | self.n + 1
| ------ ^ - {integer}
| |
| T
|
= note: `T` might need a bound for `std::ops::Add`
T
is aString
?snake_case
for variables, methods, macros, fields and modules;UpperCamelCase
for types and enum variants; andSCREAMING_SNAKE_CASE
for statics and constants.