Skip to content

Instantly share code, notes, and snippets.

@bwindels
Created August 29, 2018 12:04
Show Gist options
  • Save bwindels/3572a948ee3079f43c6cdb899843215e to your computer and use it in GitHub Desktop.
Save bwindels/3572a948ee3079f43c6cdb899843215e to your computer and use it in GitHub Desktop.
rust struct that works for both String and &str members
struct Event<S> {
id: S
}
impl<T: AsRef<str>> Event<T> {
fn print(&self) {
println!("event({})", self.id.as_ref());
}
}
fn main() {
let owned_event = Event { id: String::from("owned_id") };
owned_event.print();
let borrowed_event = Event { id: "borrowed_id" };
borrowed_event.print();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment