Skip to content

Instantly share code, notes, and snippets.

@carloscasalar
Forked from rust-play/playground.rs
Last active July 30, 2018 15:50
Show Gist options
  • Save carloscasalar/5ec57c1bd19d53125fc3720c815eda00 to your computer and use it in GitHub Desktop.
Save carloscasalar/5ec57c1bd19d53125fc3720c815eda00 to your computer and use it in GitHub Desktop.
Sample usage of associative type in rust
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