[筆記][rust] struct範例code

結構這部分跟大部分的語言都有相同的東西,所以也沒什麼好寫了。
https://doc.rust-lang.org/rust-by-example/custom_types/structs.html
https://kaisery.github.io/trpl-zh-cn/ch05-01-defining-structs.html




結果:


1. basically
user1 => User { username: "anotherusername567", email: "another@example.com" }
user1.email => another@example.com
user1.username => anotherusername567
使用..語法指定了剩餘未設置的欄位與給定對應字段相同的值
user2 => User { username: "anotherusername567", email: "xxxx@example.com" }
user2.email => xxxx@example.com
user2.username => anotherusername567

2. A tuple struct
carprice => CarPrice("Benz", 2000)
car name Benz , car price 2000
Structs can be reused as fields of another struct
car box => CarBox { top: CarPrice("Benz", 2000), bottom: CarPrice("BMW", 1500) }

destructure a tuple struct
car name => Benz
car price => 2000

3. A unit struct
nil => Nil

留言

熱門文章