0

I am trying to use generics in Rust and have encountered a probem when trying to define a function.

Let a method get take in a Vector where its type is generic, for example

fn get<T1: serde::Serialize, T2: serde::Serialize>(input: Vec<T>)
where T can be either T1 or T2

I am trying to define the function properly especially I am stuck at the point where I need to define T such that it can either be T1 or T2.

Is this possible in Rust?

babla34
  • 15
  • 4
  • 1
    Specifically about two types, there is the [`either`](https://docs.rs/either) crate that also implements a bunch of useful traits and methods. – Chayim Friedman Dec 14 '22 at 09:24
  • The either crate seems to be useful. I will use it. Thanks! – babla34 Dec 14 '22 at 09:38
  • You can simply define an enum that contains variants of these types, or define a trait an implement if for both. `T1` and `T2` seem to be the same in your example tho – Miiao Dec 14 '22 at 11:52

0 Answers0