佛山网站策划哪家专业,网站降权怎么处理,宁波网站开发定制,wordpress无法写文章一、结构定义 struct-翻译为结构/结构体
总体上有两种定义方式#xff1a;带有详细属性名的#xff1b;不带属性名#xff08;元组#xff09;
从工程角度出发#xff0c;并不推荐不带属性的定义方式#xff0c;因为不友好。希望rust后面不要搞类似好像很友好#xff…一、结构定义 struct-翻译为结构/结构体
总体上有两种定义方式带有详细属性名的不带属性名元组
从工程角度出发并不推荐不带属性的定义方式因为不友好。希望rust后面不要搞类似好像很友好但是其实起到干扰作用的语法。
如果再考虑到一些rust的其它问题定义一个结构其实也不是那么容易。
示例 struct Point{x:f64,y:f64} struct Triangle(Point,Point,Point);
/** * 家庭结构体s */ struct Family { name: String, father: String, mather: String, children: VecString, } /** * 这都什么狗屎语法。 */ struct Booka { name: a str, author: a str, price: f64, pubyear: i32, } fn main() { let mut family Family { name: String::from(家), father: String::from(爸爸), mather: String::from(妈妈), children: vec![], }; family.children.push(String::from(老大-独龙)); family.children.push(String::from(老二-独眼)); family.children.push(String::from(老三-独秀)); family.children.push(String::from(老四-嘟嘟)); family.children.push(String::from(老五-杜牧)); //打印家庭结构体 println!( 家庭:{},父亲:{},母亲:{}, family.name, family.father, family.mather ); for child in family.children { println!(孩子:{}, child); } let mut 三国演义 Book { name: 三国演义, author: 罗贯中, price: 29.8, pubyear: 1300, }; //各种创建结构实例的方式 // 方式一双点号复制 let mut 红楼梦Book{ name:红楼梦, author:曹雪芹, ..三国演义 }; //方式二: 略属性名 let 西游记write_book(西游记,吴承恩,24.0,1525); //方式三:使用基于元组定义的。 比java的record还简单 let books[三国演义,红楼梦,西游记]; for i in 0..books.len() { print_book(books[i]); } let p1Point{x:10.0,y:20.0}; let p2Point{x:20.0,y:20.0}; let p3Point{x:20.0,y:10.0}; let tTriangle(p1,p2,p3); print_triangle(t); }
fn print_book(book:Book){ println!( 书名:{},作者:{},价格:{},出版年:{}, book.name, book.author, book.price, book.pubyear ); }
fn print_triangle(t:Triangle){ println!(三点坐标:); println!({},{},t.0.x,t.0.y); println!({},{},t.1.x,t.1.y); println!({},{},t.2.x,t.2.y); }
fn write_booka(name:a str,author:a str,price:f64,pubyear:i32)-Booka{ Book{ name, author, price, pubyear } }
在上例中结构Book使用了非常奇怪的语法 struct Booka { name: a str, author: a str, price: f64, pubyear: i32,
}
这个能够定义出来是因为编译器提示的。
作为初学者先绕过这个吧。
rust结构体实例的属性赋值也有两种方式 属性逐一赋值
双点号复制属性值 例如以下就是 let mut 红楼梦Book{ name:红楼梦, author:曹雪芹, ..三国演义 };
二、几种打印方式 至少有4种打印方式
逐一访问属性名 println!使用宏符号:? println!使用宏符号:#? 使用dbg! 后面三种方式要求定义结构的时候在结构前添加
#[derive(Debug)] 这个东西应该怎么称呼了 属性还是编译指示符?有点乱七八杂的。
从宏观上而言很多类似的都是可以称为编译指示符所以为了更加精准一些我愿意称为功能编译指示。
通过这个功能编译指示rust编译器会自动实现特定功能。
示例 #[derive(Debug)] struct Family{ father:String, mather:String, address:String }
fn main(){ let mut mfFamily{ father:String::from(lu), mather:String::from(hu), address:String::from(中国) }; println!(我家-{},{},{},mf.father,mf.mather,mf.address); print_family(mf); print_family_use_dbg(mf); }
fn print_family(f:Family){ //你不能直接打印否则会有奇奇怪怪的错误提示 //println!({},f); // 这个会提示错误所以注释掉了 //使用奇怪符号 :?打印结构体 println!(我家:?-{:?},f); //使用奇怪的符号可以打印结构体 :#? println!(我家:#?-{:#?},f); }
fn print_family_use_dbg(f:Family){ dbg!(f); }
三、定义结构内的函数 在没有看书本正文之前我以为和java的record一样在struct内部定义函数。
其实不是 如果要为结构体定义函数必须在结构体外。 不知道为什么要那样 难道内部定义的话有其它用途
示例 #[derive(Debug)] struct Cube{ length: u32, width: u32, height: u32, } impl Cube{ fn volume(self) - u32{ return self.length * self.width * self.height; } fn is_bigger_than(self, other: Cube) - bool{ return self.volume() other.volume(); }
}
fn main() { let cube Cube{length: 10, width: 12, height: 25}; let cube2 Cube{length: 15, width: 10, height: 30}; println!(立方体的体积{}立方厘米,cube.volume()); let is_bigger cube.is_bigger_than(cube2); match is_bigger{ true println!(cube的体积{}大于cube2体积{},cube.volume(), cube2.volume()), false println!(cube的体积{}小于cube2体积{},cube.volume(), cube2.volume()), }; }
结构体的函数有几个特点
在结构体外使用impl xxxx {}的方式其中xxx是结构体名称 在一个impl xxx{}结构中可以定义多个函数 书本建议我们函数的第一个方法总是 self,这点和python有点类似 参数self虽然有定义但是调用的时候不需要显示传递因为这是编译器实现的
四、一点小补充 4.1定义没有成员的结构 在很多OOP允许我们定义没有成员的对象。
我们可以把struct大体当做对象。
在rust中也可以定义没有成员的结构例如
#[derive(Debug)] struct NoItem; struct NoItem2();
let nodNoItem{}; println!(空结构体:{:?},nod);
rust允许这样做是因为struct这个类型具有很多作用即使它不用于存储数据也有很多作用。具体什么作用只能等待深入后了解。
总之在rust中绝大部分复杂的类型都是基于结构定义的。
4.2struct大有作用 前有言某种程度上可以把struct视为OOP中的对象
虽然rust不是明面上的OOP但还是吸收了很多OOP的营养。
总之许多复杂类型都是基于结构定义的。具体不一一罗列。
五、小结 结构体无疑是一个有用的东西它就算垃圾袋/宝物袋一样什么都可以往里装大大方便了工程师 关注灵活就业新业态关注公账号贤才宝 文章转载自: http://www.morning.crqbt.cn.gov.cn.crqbt.cn http://www.morning.brkrt.cn.gov.cn.brkrt.cn http://www.morning.xjqhh.cn.gov.cn.xjqhh.cn http://www.morning.lpnb.cn.gov.cn.lpnb.cn http://www.morning.rttp.cn.gov.cn.rttp.cn http://www.morning.wqpsf.cn.gov.cn.wqpsf.cn http://www.morning.wjmb.cn.gov.cn.wjmb.cn http://www.morning.yxshp.cn.gov.cn.yxshp.cn http://www.morning.zmwd.cn.gov.cn.zmwd.cn http://www.morning.qyxwy.cn.gov.cn.qyxwy.cn http://www.morning.tbqxh.cn.gov.cn.tbqxh.cn http://www.morning.jrhmh.cn.gov.cn.jrhmh.cn http://www.morning.qkskm.cn.gov.cn.qkskm.cn http://www.morning.cmqrg.cn.gov.cn.cmqrg.cn http://www.morning.gwjsm.cn.gov.cn.gwjsm.cn http://www.morning.wnhml.cn.gov.cn.wnhml.cn http://www.morning.lzqxb.cn.gov.cn.lzqxb.cn http://www.morning.lcxdm.cn.gov.cn.lcxdm.cn http://www.morning.wyfpc.cn.gov.cn.wyfpc.cn http://www.morning.iznek.com.gov.cn.iznek.com http://www.morning.pzjfz.cn.gov.cn.pzjfz.cn http://www.morning.kxqfz.cn.gov.cn.kxqfz.cn http://www.morning.jstggt.cn.gov.cn.jstggt.cn http://www.morning.bqnhh.cn.gov.cn.bqnhh.cn http://www.morning.qdzqf.cn.gov.cn.qdzqf.cn http://www.morning.drnfc.cn.gov.cn.drnfc.cn http://www.morning.wdjcr.cn.gov.cn.wdjcr.cn http://www.morning.drtgt.cn.gov.cn.drtgt.cn http://www.morning.gl-group.cn.gov.cn.gl-group.cn http://www.morning.qphdp.cn.gov.cn.qphdp.cn http://www.morning.zffps.cn.gov.cn.zffps.cn http://www.morning.zsrdp.cn.gov.cn.zsrdp.cn http://www.morning.wjplr.cn.gov.cn.wjplr.cn http://www.morning.bnbtp.cn.gov.cn.bnbtp.cn http://www.morning.cwwts.cn.gov.cn.cwwts.cn http://www.morning.zdmlt.cn.gov.cn.zdmlt.cn http://www.morning.ymwrs.cn.gov.cn.ymwrs.cn http://www.morning.rmfwh.cn.gov.cn.rmfwh.cn http://www.morning.xbtlt.cn.gov.cn.xbtlt.cn http://www.morning.mxdhy.cn.gov.cn.mxdhy.cn http://www.morning.jhqcr.cn.gov.cn.jhqcr.cn http://www.morning.brrxz.cn.gov.cn.brrxz.cn http://www.morning.gnbtp.cn.gov.cn.gnbtp.cn http://www.morning.cwqrj.cn.gov.cn.cwqrj.cn http://www.morning.lnbcx.cn.gov.cn.lnbcx.cn http://www.morning.thrgp.cn.gov.cn.thrgp.cn http://www.morning.trqhd.cn.gov.cn.trqhd.cn http://www.morning.wbxtx.cn.gov.cn.wbxtx.cn http://www.morning.ctfh.cn.gov.cn.ctfh.cn http://www.morning.jyjqh.cn.gov.cn.jyjqh.cn http://www.morning.tldhq.cn.gov.cn.tldhq.cn http://www.morning.nnmnz.cn.gov.cn.nnmnz.cn http://www.morning.rptdz.cn.gov.cn.rptdz.cn http://www.morning.dxqwm.cn.gov.cn.dxqwm.cn http://www.morning.njftk.cn.gov.cn.njftk.cn http://www.morning.jxwhr.cn.gov.cn.jxwhr.cn http://www.morning.nwmwp.cn.gov.cn.nwmwp.cn http://www.morning.srrzb.cn.gov.cn.srrzb.cn http://www.morning.rcdmp.cn.gov.cn.rcdmp.cn http://www.morning.pqfbk.cn.gov.cn.pqfbk.cn http://www.morning.pznqt.cn.gov.cn.pznqt.cn http://www.morning.ftmzy.cn.gov.cn.ftmzy.cn http://www.morning.pswqx.cn.gov.cn.pswqx.cn http://www.morning.pbtdr.cn.gov.cn.pbtdr.cn http://www.morning.ppdr.cn.gov.cn.ppdr.cn http://www.morning.dkzrs.cn.gov.cn.dkzrs.cn http://www.morning.fylqz.cn.gov.cn.fylqz.cn http://www.morning.pinngee.com.gov.cn.pinngee.com http://www.morning.gqfks.cn.gov.cn.gqfks.cn http://www.morning.nqyfm.cn.gov.cn.nqyfm.cn http://www.morning.syrzl.cn.gov.cn.syrzl.cn http://www.morning.gbsby.cn.gov.cn.gbsby.cn http://www.morning.gwjqq.cn.gov.cn.gwjqq.cn http://www.morning.zpstm.cn.gov.cn.zpstm.cn http://www.morning.ghrhb.cn.gov.cn.ghrhb.cn http://www.morning.zgztn.cn.gov.cn.zgztn.cn http://www.morning.flpjy.cn.gov.cn.flpjy.cn http://www.morning.trsfm.cn.gov.cn.trsfm.cn http://www.morning.bslkt.cn.gov.cn.bslkt.cn http://www.morning.plzgt.cn.gov.cn.plzgt.cn