Skip to content

Instantly share code, notes, and snippets.

@ircnelson
Forked from anonymous/playground.rs
Created April 6, 2017 16:29
Show Gist options
  • Save ircnelson/ff37e0af13d2bae401b70df70a6c47e5 to your computer and use it in GitHub Desktop.
Save ircnelson/ff37e0af13d2bae401b70df70a6c47e5 to your computer and use it in GitHub Desktop.
Rust Lifetime Playground
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