struct Person { first_name: String, last_name: String, } impl Person { fn new(first_name: String, last_name: String) -> Self { Person {first_name, last_name} } fn greet(&self) -> String { format!("Hello, my name is {} {}", self.first_name, self.last_name) } }
- struct Person {
- first_name: String,
- last_name: String,
- }
- impl Person {
fn greet(&self) -> String {format!("Hello, my name is {} {}", self.first_name, self.last_name)}}fn person_builder(first_name: &str, last_name: &str) -> Person {Person { first_name: first_name.to_owned(), last_name: last_name.to_owned() }- fn new(first_name: String, last_name: String) -> Self {
- Person {first_name, last_name}
- }
- fn greet(&self) -> String {
- format!("Hello, my name is {} {}", self.first_name, self.last_name)
- }
- }
#[test] fn can_build_a_person() { let p = Person::new("Jimbo".to_string(), "Jones".to_string()); assert_eq!(p.greet(), "Hello, my name is Jimbo Jones"); } #[test] #[should_panic] fn failed_name() { let p = Person::new("Bill".to_string(), "Smith".to_string()); assert_eq!(&p.first_name, "Frank"); }
- #[test]
- fn can_build_a_person() {
let p = person_builder("Jimbo", "Jones");- let p = Person::new("Jimbo".to_string(), "Jones".to_string());
- assert_eq!(p.greet(), "Hello, my name is Jimbo Jones");
- }
- #[test]
- #[should_panic]
- fn failed_name() {
let p = person_builder("Bill", "Smith");- let p = Person::new("Bill".to_string(), "Smith".to_string());
- assert_eq!(&p.first_name, "Frank");
- }