Skip to content

Instantly share code, notes, and snippets.

@grahamking
Created January 31, 2021 04:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grahamking/c6e199cb5fde0773f68f6d34f2469492 to your computer and use it in GitHub Desktop.
Save grahamking/c6e199cb5fde0773f68f6d34f2469492 to your computer and use it in GitHub Desktop.
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