Skip to content

Instantly share code, notes, and snippets.

@stevej
Forked from anonymous/playground.rs
Created January 8, 2017 21:24
Show Gist options
  • Save stevej/426793e8b0e2c54231d2b544c1db49c4 to your computer and use it in GitHub Desktop.
Save stevej/426793e8b0e2c54231d2b544c1db49c4 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
// In Rust, much like Go, you organize functions around structs.
#[derive(Debug)]
struct Person {
id: u64,
name: String,
twitter_handle: String
}
// Here we hang a function off of a struct.
impl Person {
fn twitter_url(&self) -> String {
return "http://twitter.com/".to_string() + &self.twitter_handle;
}
}
fn main() {
// str literals (e.g. "Buoyant") need to be converted into proper String objects.
// http://www.steveklabnik.com/rust-issue-17340/#string-vs.-&str
let stevej = Person { id: 1, name: "Steve Jenson".to_string(), twitter_handle: "@stevej".to_string() };
println!("{:?} -> {:?}", stevej.name, stevej.twitter_url());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment