Last active
January 9, 2017 17:27
-
-
Save stevej/a4d2594fa42a1bf41b08973a1659444e to your computer and use it in GitHub Desktop.
Rust for the Experienced Programmer. Lesson 1: The Basics
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 { | |
// Here we make a choice to use a reference. This choice will be explored more later on. | |
fn twitter_url(&self) -> String { | |
return "http://twitter.com/".to_string() + &self.twitter_handle; | |
} | |
} | |
fn main() { | |
// str literals (e.g. "Buoyant") are slices and 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() }; | |
// functions ending in ! are macros. | |
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=a4d2594fa42a1bf41b08973a1659444e