Skip to content

Instantly share code, notes, and snippets.

@matematikaadit
Created January 6, 2017 16:21
Show Gist options
  • Save matematikaadit/2af6324d19f3e467c1e1016f882247dc to your computer and use it in GitHub Desktop.
Save matematikaadit/2af6324d19f3e467c1e1016f882247dc to your computer and use it in GitHub Desktop.
trait Next<S> {
fn next(&mut self) -> S;
}
#[derive(Debug)]
struct Multi {
s: &'static str,
n: u8,
x: f32,
}
impl Next<&'static str> for Multi {
fn next(&mut self) -> &'static str {
self.s
}
}
impl Next<u8> for Multi {
fn next(&mut self) -> u8 {
self.n
}
}
impl Next<f32> for Multi {
fn next(&mut self) -> f32 {
self.x
}
}
fn main() {
let mut multi = Multi { s: "Hello, World!", n: 42, x: 0.3 };
println!("{:?}", multi);
let n: u8 = multi.next();
println!("{:?}", n);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment