-
-
Save carloscasalar/5ec57c1bd19d53125fc3720c815eda00 to your computer and use it in GitHub Desktop.
Sample usage of associative type in rust
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
pub trait Point{ | |
type Identifier: PartialEq; | |
fn id(&self) -> Self::Identifier; | |
} | |
#[derive(Debug)] | |
pub struct Connection<T: Point> { | |
pub to: T | |
} | |
impl<T: Point> Connection<T> { | |
pub fn is_connected_to(&self, point: T) -> bool | |
{ | |
self.to.id() == point.id() | |
} | |
} | |
pub fn main() { | |
#[derive(Debug)] | |
struct SimplePoint; | |
impl Point for SimplePoint{ | |
type Identifier = char; | |
fn id(&self) -> char { return 'A' } | |
} | |
let a = SimplePoint {}; | |
let conn = Connection { | |
to: a | |
}; | |
println!("conn: {:?}", conn); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment