Created
January 7, 2017 00:04
-
-
Save anonymous/d6435ffcf9676382fec33b2834972c78 to your computer and use it in GitHub Desktop.
Shared via Rust 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
// 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
https://play.rust-lang.org/?gist=d6435ffcf9676382fec33b2834972c78&version=stable&backtrace=0