Created
August 29, 2018 12:04
-
-
Save bwindels/3572a948ee3079f43c6cdb899843215e to your computer and use it in GitHub Desktop.
rust struct that works for both String and &str members
This file contains hidden or 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
| 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