traits3.rs (794B)
1 trait Licensed { 2 fn licensing_info(&self) -> String { 3 "Default license".to_string() 4 } 5 } 6 7 struct SomeSoftware { 8 version_number: i32, 9 } 10 11 struct OtherSoftware { 12 version_number: String, 13 } 14 15 impl Licensed for SomeSoftware {} 16 impl Licensed for OtherSoftware {} 17 18 fn main() { 19 // You can optionally experiment here. 20 } 21 22 #[cfg(test)] 23 mod tests { 24 use super::*; 25 26 #[test] 27 fn is_licensing_info_the_same() { 28 let licensing_info = "Default license"; 29 let some_software = SomeSoftware { version_number: 1 }; 30 let other_software = OtherSoftware { 31 version_number: "v2.0.0".to_string(), 32 }; 33 assert_eq!(some_software.licensing_info(), licensing_info); 34 assert_eq!(other_software.licensing_info(), licensing_info); 35 } 36 }