๊ตฌ์กฐ์ฒด

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.
    • ๋‹ค์Œ ์Šฌ๋ผ์ด๋“œ์—์„œ๋Š” ํ•„๋“œ ์ด๋ฆ„์ด ๋œ ์ค‘์š”ํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํŠœํ”Œ ๊ตฌ์กฐ์ฒด๋ฅผ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค.
  • 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.