Skip to content

Instantly share code, notes, and snippets.

@stevej
Last active January 9, 2017 17:27
Show Gist options
  • Save stevej/a4d2594fa42a1bf41b08973a1659444e to your computer and use it in GitHub Desktop.
Save stevej/a4d2594fa42a1bf41b08973a1659444e to your computer and use it in GitHub Desktop.
Rust for the Experienced Programmer. Lesson 1: The Basics
// 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());
}
@stevej
Copy link
Author

stevej commented Jan 9, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment