rust 结构体方法和关联函数
作者:互联网
1 struct Targ { 2 w:u32, 3 h:u32, 4 } 5 impl Targ { 6 fn aera(&self) -> u32 { 7 self.w * self.h 8 } 9 fn sround(&self) -> u32 { 10 (self.w + self.h ) * 2 11 } 12 fn init(x:u32, y:u32) -> Targ { 13 Targ{w:x, h:y} 14 } 15 } 16 fn main(){ 17 //let t1 = Targ{w:20, h:15}; 18 let t1 = Targ::init(25, 8); 19 println!("{}\t{}",t1.aera(),t1.sround()); 20 println!("{}\t{}",&t1.aera(),&t1.sround()); 21 }
标签:sround,函数,self,Targ,关联,u32,fn,t1,rust 来源: https://www.cnblogs.com/mmtinfo/p/15665450.html