Working with generic type

Let;s say I've this simple function, that is receiving 2 parameters of the same type, in this case, it is i32.

How can I make this function general for any two numbers under the condition of being of the same type, mmm how also can I define it, if I need them not to be from the same type?

fn foo(x: i32, y: i32) -> i32 {
}

And is there any difference if the input is Vec<T> or Vec<Vec>`

https://doc.rust-lang.org/book/ch10-00-generics.html

1 Like

Here is a fast example for generic function: Rust Playground

You can't do it for any numbers in any useful sense. Rust has generics for types that can be anything, which can't do anything number-specific, unless you painfully meticulously declare every single operation, in full detail, that you want to perform on these numbers.

In practice I suggest staying far away from generics on numbers, and using a macro to duplicate methods.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.