Created
August 4, 2015 07:43
-
-
Save sumproxy/634a539f9aa98cd46f96 to your computer and use it in GitHub Desktop.
This file contains 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
struct Container<A, B, T>(A, B); | |
// A trait which will check to see if two items are stored inside of container. | |
// Also retrieves first or last value. | |
trait Contains { | |
// Define generic types here which methods will be able utilize. | |
type A; | |
type B; | |
type T; | |
fn contains(&self, &Self::A, &Self::B) -> bool; | |
fn first(&self) -> Self::A; | |
fn last(&self) -> Self::B; | |
fn difference(&self) -> Self::T; | |
} | |
impl<A, B, T> Contains for Container<A, B, T> | |
{ | |
// Specify what types `A` and `B` are. If the `input` type | |
// is `Container(i32, i32)`, the `output` types are determined | |
// as `i32` and `i32`. | |
type T = ? | |
type A = T;// = i32; | |
type B = T;// = i32; | |
// `&Self::A` and `&self::B` are also valid here. | |
fn contains(&self, number: &Self::A, digit: &Self::B) -> bool { | |
(&self.0 == number) && (&self.1 == digit) | |
} | |
// Grab the first number. | |
fn first(&self) -> Self::A { self.0 } | |
// Grab the last number. | |
fn last(&self) -> Self::B { self.1 } | |
// Compute the difference | |
fn difference(&self) -> Self::T { | |
self.first() - self.last() | |
} | |
} | |
fn main() { | |
let number = 3; | |
let digit = 10; | |
let container = Container(number, digit); | |
println!("Does container contain {} and {}: {}", | |
&number, &digit, | |
container.contains(&number, &digit)); | |
println!("First number: {}", container.first()); | |
println!("Last number: {}", container.last()); | |
//println!("The difference is: {}", difference(&container)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment