Last active
January 8, 2017 10:48
-
-
Save dumindu/faf5e4204965efee8d18a53aae2e80dd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// generalizing functions | |
//----------------------- | |
fn takes_anything<T>(x: T) { // x has type T, T is a generic type | |
} | |
fn takes_two_of_the_same_things<T>(x: T, y: T) { // both x and y has same type | |
} | |
fn takes_two_things<T, U>(x: T, y: U) { // multiple types | |
} | |
// generalizing structs | |
//--------------------- | |
struct Point<T> { | |
x: T, | |
y: T, | |
} | |
fn main() { | |
let point_a = Point { x: 0, y: 0 }; // T is a int type | |
let point_b = Point { x: 0.0, y: 0.0 }; // T is a float type | |
} | |
// 🔎 When addding an implementation for a generic struct, the type parameters should be declared after the impl as well | |
// impl<T> Point<T> { | |
// generalizing enums | |
//------------------- | |
enum Option<T> { | |
Some(T), | |
None, | |
} | |
enum Result<T, E> { | |
Ok(T), | |
Err(E), | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment