Skip to content

Instantly share code, notes, and snippets.

@aneurysmjs
Last active November 10, 2022 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aneurysmjs/7465f3d79d5a9e31e37a3ef2e835cbdd to your computer and use it in GitHub Desktop.
Save aneurysmjs/7465f3d79d5a9e31e37a3ef2e835cbdd to your computer and use it in GitHub Desktop.
Generics in Rust
struct Point<T, U> {
x: T,
y: U,
}
impl<T, U> Point<T, U> {
// Methods that use different generic types than their struct’s definition
fn mixup<V, W>(self, other: Point<V, W>) -> Point<T, W> {
Point {
x: self.x,
y: other.y,
}
}
}
let p1 = Point { x: 5, y: 10.4 };
let p2 = Point { x: "Hello", y: 'c'};
let p3 = p1.mixup(p2);
println!("p3.x = {}, p3.y = {}", p3.x, p3.y); // p3.x = 5, p3.y = c
// ------------- //
pub trait Summarizable {
fn author_summary(&self) -> String;
fn summary(&self) -> String {
format!("(Read more from {}...)", self.author_summary()) // Default Implementations
}
}
// To be able to call `summary` on `item` without getting an error,
// we can use trait bounds on T to specify that item must be of a
// type that implements the Summarizable trait
pub fn notify<T: Summarizable>(item: T) {
println!("Breaking news! {}", item.summary());
}
// We can specify multiple trait bounds on a generic type by using +.
// If we needed to be able to use display formatting on the type T
// in a function as well as the summary method, we can use the
// trait bounds T: Summarizable + Display.
// This means T can be any type that implements both Summarizable and Display.
// For functions that have multiple generic type parameters, each generic has its own trait bounds.
// Specifying lots of trait bound information in the angle brackets between a function’s name and
// its parameter list can get hard to read, so there’s an alternate syntax for specifying
// trait bounds that lets us move them to a where clause after the function signature. So instead of:
fn some_function<T: Display + Clone, U: Clone + Debug>(t: T, u: U) -> i32 {...}
// We can write this instead with a `where` clause:
fn some_function<T, U>(t: T, u: U) -> i32
where T: Display + Clone,
U: Clone + Debug
{...}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment