1

I have a function test that calculates the sum of two variables of type N, returning N. How can I specify N::Output type must be N?

use std::ops::Add;

fn test<N: Add>(num_a: N, num_b: N) -> N {
    num_a + num_b
}

fn main() {
    println!("{:?}", test(5, 7));
}

Playground

error[E0308]: mismatched types
 --> src/main.rs:4:5
  |
3 | fn test<N: Add>(num_a: N, num_b: N) -> N {
  |                                        - expected `N` because of return type
4 |     num_a + num_b
  |     ^^^^^^^^^^^^^ expected type parameter, found associated type
  |
  = note: expected type `N`
             found type `<N as std::ops::Add>::Output`
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
jiwopene
  • 3,077
  • 17
  • 30

0 Answers0