-
-
Save ircnelson/ff37e0af13d2bae401b70df70a6c47e5 to your computer and use it in GitHub Desktop.
Rust Lifetime Playground
This file contains 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
use std::collections::HashMap; | |
fn main() { | |
struct User<'a> { | |
name: &'a str, | |
} | |
impl<'a> User<'a> { | |
fn new(uname: &'a str, pwd: &'a str) -> User<'a> { | |
User { name: uname } | |
} | |
} | |
struct ChatRoom<'a> { | |
name: &'a str, | |
users: HashMap<&'a str, User<'a>>, | |
} | |
impl<'a> ChatRoom<'a> { | |
fn new(name: &str) -> ChatRoom { | |
let users = HashMap::new(); | |
ChatRoom { | |
name: name, | |
users: users, | |
} | |
} | |
fn join(&mut self, user: User<'a>) { | |
self.users.insert(user.name, user); | |
} | |
} | |
let mut room = ChatRoom::new("Test"); | |
let user = User::new("bender", "123"); | |
room.join(user); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment