Skip to content

Instantly share code, notes, and snippets.

Created January 7, 2017 00:04
Show Gist options
  • Save anonymous/d6435ffcf9676382fec33b2834972c78 to your computer and use it in GitHub Desktop.
Save anonymous/d6435ffcf9676382fec33b2834972c78 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