๊ตฌ์กฐ์ฒด
C/C++ ์ ๋ง์ฐฌ๊ฐ์ง๋ก ๋ฌ์คํธ๋ ์ปค์คํ ๊ตฌ์กฐ์ฒด๋ฅผ ์ง์ํฉ๋๋ค:
struct Person { name: String, age: u8, } fn describe(person: &Person) { println!("{}์(๋) {}์ธ์ ๋๋ค.", person.name, person.age); } fn main() { let mut peter = Person { name: String::from("ํผํฐ"), age: 27 }; describe(&peter); peter.age = 28; describe(&peter); let name = String::from("์์ด๋ฒ๋ฆฌ"); let age = 39; let avery = Person { name, age }; describe(&avery); let jackie = Person { name: String::from("์ฌํค"), ..avery }; describe(&jackie); }
This slide should take about 10 minutes.
ํค ํฌ์ธํธ:
- ๊ตฌ์กฐ์ฒด๋ C/C++ ์ ์ ์ฌํฉ๋๋ค.
- C++ ์ ๊ฐ์ง๋ง C์๋ ๋ฌ๋ฆฌ ํ์ ์ ์ ์ํ๊ธฐ ์ํด โtypedefโ๊ฐ ํ์ํ์ง ์์ต๋๋ค.
- C++ ์ ๋ฌ๋ฆฌ ๊ตฌ์กฐ์ฒด ๊ฐ ์์์ ์์ต๋๋ค.
- This may be a good time to let people know there are different types of structs.
- Zero-sized structs (e.g.
struct Foo;
) might be used when implementing a trait on some type but donโt have any data that you want to store in the value itself. - ๋ค์ ์ฌ๋ผ์ด๋์์๋ ํ๋ ์ด๋ฆ์ด ๋ ์ค์ํ ๋ ์ฌ์ฉํ ์ ์๋ ํํ ๊ตฌ์กฐ์ฒด๋ฅผ ์๊ฐํฉ๋๋ค.
- Zero-sized structs (e.g.
- If you already have variables with the right names, then you can create the struct using a shorthand.
- The syntax
..avery
allows us to copy the majority of the fields from the old struct without having to explicitly type it all out. It must always be the last element.