Skip to content

Instantly share code, notes, and snippets.

@grahamking grahamking/objects.rs
Last active Dec 16, 2015

Embed
What would you like to do?
Rust: Objects
extern mod std;
use std::time;
struct User {
name: ~str,
age: int
}
impl User {
fn new(name: ~str, age: int) -> User {
User{name: name, age:age}
}
fn say_hi(&self) {
println(fmt!("%s greets you.", self.name));
}
fn when(&self, age: int) -> int {
if age < self.age { -1 }
else { 1900 + (time::now().tm_year as int) + age - self.age }
}
}
fn main() {
//let u1 = User{name: ~"Bob", age: 36};
let u1 = User::new(~"Bob", 36);
u1.say_hi();
println(fmt!("%s will be 40 in %d", u1.name, u1.when(40)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.