Last active
August 29, 2015 14:09
-
-
Save justincampbell/ff08b1112f22f812c906 to your computer and use it in GitHub Desktop.
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
use std::collections::HashMap; | |
type Token = String; | |
type Url = String; | |
struct World { id: int, urls: HashMap<Token, Url> } | |
fn main() { | |
let mut world = World { urls: HashMap::new(), id: 0i }; | |
let url = "justincampbell.me".to_string(); | |
print!("{} -> ", url); | |
let token = world.shorten(url); | |
print!("{} -> ", token); | |
match world.expand(token) { | |
Some(expanded_url) => println!("{}", expanded_url), | |
None => println!("Could not find url for token") | |
// Need to figure out how to keep an immutable copy of token after we | |
// give it to expand() | |
// None => println!("Could not find url for token {}", token) | |
} | |
} | |
impl World { | |
fn shorten(&mut self, url: Url) -> Token { | |
self.id += 1; | |
let token = World::tokenize(self.id); | |
self.urls.insert(token.to_string(), url); | |
token | |
} | |
fn expand(&self, token: Token) -> Option<&Url> { | |
self.urls.get(&token) | |
} | |
fn tokenize(id: int) -> String { | |
id.to_string() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment