Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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