Created
November 9, 2015 17:57
-
-
Save carlosgaldino/687a2363b158fd1c37a1 to your computer and use it in GitHub Desktop.
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
struct Person { | |
name: String, | |
age: u8 | |
} | |
fn main() { | |
// only works if compiled with `--test` flag. | |
// let p = Person::create_with(String::from("Carlos"), 26); | |
let p = Person { name: String::from("Carlos"), age: 26 }; | |
println!("{} {}", p.name, p.age); | |
} | |
#[cfg(test)] | |
impl Person { | |
fn create_default() -> Person { | |
Person { name: String::from("John"), age: 28 } | |
} | |
fn create_with(name: &str, age: u8) -> Person { | |
Person { name: String::from(name), age: age } | |
} | |
} | |
#[test] | |
fn default_is_valid() { | |
let p = Person::create_default(); | |
assert_eq!(p.name, "John"); | |
assert_eq!(p.age, 29); // this will fail | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment