Created
January 31, 2021 04:42
-
-
Save grahamking/c6e199cb5fde0773f68f6d34f2469492 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
trait Position<T> { | |
fn pos(&self) -> T; | |
} | |
struct Location { | |
lat: f32, | |
lon: f32, | |
} | |
impl Position<String> for Location { | |
fn pos(&self) -> String { | |
format!("{},{}", self.lat, self.lon) | |
} | |
} | |
#[derive(Debug)] | |
struct Point { | |
x: f32, | |
y: f32, | |
} | |
impl Position<Point> for Location { | |
fn pos(&self) -> Point { | |
Point { | |
x: self.lat, | |
y: self.lon, | |
} | |
} | |
} | |
fn main() { | |
let l = Location { | |
lat: 37.05655, | |
lon: -121.88823, | |
}; | |
let s: String = l.pos(); | |
let p: Point = l.pos(); | |
println!("As string: {}", s); | |
println!("As point: {:?}", p); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment