Created
April 11, 2015 00:39
-
-
Save jakejscott/ca6b4b3cfed649cf71aa to your computer and use it in GitHub Desktop.
Playing around with rust
This file contains hidden or 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
use std::fmt; | |
#[derive(Debug)] | |
struct Person { | |
name: String, | |
age: i32, | |
} | |
impl fmt::Display for Person { | |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
write!(f, "({}, {})", self.name, self.age) | |
} | |
} | |
fn make_vec() -> Vec<i32> { | |
let mut vec = Vec::new(); | |
vec.push(0); | |
vec.push(1); | |
vec | |
} | |
fn make_person() -> Person { | |
let person = Person { | |
name: "Jake".to_string(), | |
age: 33 | |
}; | |
person | |
} | |
fn print_vec(vec: &Vec<i32>) { | |
for i in vec.iter() { | |
println!("{}", i) | |
} | |
} | |
fn debug_person(p: &Person) { | |
println!("{:?}", p); | |
} | |
fn display_person(p: &Person) { | |
println!("{}", p); | |
} | |
fn use_vec() { | |
let vec = make_vec(); | |
print_vec(&vec); | |
for i in vec.iter() { | |
println!("{}", i) | |
} | |
} | |
fn use_person() { | |
let p = make_person(); | |
display_person(&p); | |
debug_person(&p); | |
let p2 = Person { | |
name: "Jake".to_string(), | |
age: 33 | |
}; | |
display_person(&p2); | |
debug_person(&p2); | |
} | |
fn push_all(from: &Vec<i32>, to: &mut Vec<i32>) { | |
for i in from.iter() { | |
to.push(*i); | |
} | |
} | |
fn main() { | |
use_vec(); | |
use_person(); | |
let v1 = make_vec(); | |
let mut v2 = Vec::new(); | |
push_all(&v1, &mut v2); | |
print_vec(&v1); | |
print_vec(&v2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment