5.0 Defining and Instantiating Structs

struct User {
    username: String,
    email: String,
    sign_in_count: u64,
    active: bool,
}
  • ๊ธฐ๋ณธ์ ์œผ๋กœ ํŠœํ”Œ๊ณผ ๋น„์Šทํ•˜๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌถ์–ด์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.
  • ํŠœํ”Œ๋ณด๋‹ค ๋” ๋งŽ์€ ์œ ์—ฐ์„ฑ์„ ์ œ๊ณตํ•œ๋‹ค.
  • cpp์˜ ๊ตฌ์กฐ์ฒด์™€ ๊ฑฐ์˜ ๋™์ผํ•˜๋‹ค.
  • ts์˜ ์ธํ„ฐํŽ˜์ด์Šค์™€ ์œ ์‚ฌํ•˜๋‹ค.
  • struct ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ •์˜ํ•œ๋‹ค.
fn main() {
  let user1 = User {
    email: String::from("some@example.com"),
    username: String::from("someusername"),
    active: true,
    sign_in_count: 1,
}
  • .์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•„๋“œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋งŒ์•ฝ instance๊ฐ€ mutableํ•˜๋‹ค๋ฉด ํ•„๋“œ์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ํ•„๋“œ์˜ ์ผ๋ถ€๋Š” mutable์ด๊ณ  ์ผ๋ถ€๋Š” immutable์ผ ์ˆ˜ ์—†๋‹ค.
fn build_user(email: String, username: String) -> User {
    User {
        email: email,
        username: username,
        active: true,
        sign_in_count: 1,
    }
}

fn build_user2(email: String, username: String) -> User {
    User {
        email,
        username,
        active: true,
        sign_in_count: 1,
    }
}
  • ํ‘œํ˜„์‹ ํ˜•ํƒœ๋กœ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ํ•„๋“œ์˜ ์ด๋ฆ„๊ณผ ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์ด ๊ฐ™๋‹ค๋ฉด email: email์„ email๋กœ ์ถ•์•ฝํ•  ์ˆ˜ ์žˆ๋‹ค.

5.1.1 Creating Instances From Other Instances With Struct Update Syntax

fn main() {
    // --snip--
    let user2 = User {
        email: String::from("another@example.com"),
        active: user1.active,
        sign_in_count: user1.sign_in_count,
        username: user1.username,
    };
    
    let user3 = User {
        email: String::from("another@example.com"),
        ..user1
    };
}
  • ..์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค๋ฅธ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ณต์‚ฌํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋””์Šค๋Ÿญ์ณ๋ง๊ณผ ๋น„์Šทํ•œ ๋ฌธ๋ฒ•์ด์ง€๋งŒ, ๊ตฌ์กฐ์ฒด๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ iterable trait๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ์ง€๋Š” ์•Š๋Š”๋‹ค๊ณ  ํ•œ๋‹ค. ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„๋˜์–ด์žˆ๋Š”์ง€ ๊ถ๊ธˆํ•˜๋‹ค. (์•Œ์•„๋ณด๊ธฐ)
  • ์†Œ์œ ๊ถŒ ์ด์ „์€ ๋™์ผํ•œ ๋…ผ๋ฆฌ๋กœ ์ผ์–ด๋‚˜๊ธฐ ๋•Œ๋ฌธ์—, ๋งŒ์•ฝ stack only data๊ฐ€ ์•„๋‹Œ ํ•„๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋ฉด, ์—…๋ฐ์ดํŠธ ์ดํ›„ ๊ธฐ์กด ์ธ์Šคํ„ด์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ฒŒ ๋œ๋‹ค.

5.1.2 Using Tuple Structs without Named Fields to Create Different Types

struct Color(i32, i32, i32);
struct Point(i32, i32, i32);

fn main() {
    let black = Color(0, 0, 0);
    let origin = Point(0, 0, 0);
}
  • ํ•„๋“œ์˜ ์ด๋ฆ„์ด ์—†๋Š” ํŠœํ”Œ ๊ตฌ์กฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ํ•„๋“œ์˜ ์ด๋ฆ„์„ ๋ถ™์ด์ง€ ์•Š๋Š” ๊ตฌ์กฐ์ฒด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

  • ํ•œ๋ฒˆ ํƒ€์ž…์„ ์ •์˜ํ•˜๋ฉด, ๊ทธ ํ•„๋“œ๋“ค์ด ๊ฐ™์•„๋„ ๋‹ค๋ฅธ ํƒ€์ž…์œผ๋กœ ์ธ์‹ํ•œ๋‹ค.

  • black๊ณผ origin์€ ์„œ๋กœ ๋‹ค๋ฅธ ํƒ€์ž…์ด๊ธฐ์—, ํ•จ์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋“ฑ์œผ๋กœ ๋ฐ˜๋Œ€๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ์ปดํŒŒ์ผ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

5.1.3 Unit-Like Structs Without Any Fields

struct UnitLikeStruct;

fn main() {
    let unit_like_struct = UnitLikeStruct;
}
  • ํ•„๋“œ๊ฐ€ ์—†๋Š” ๊ตฌ์กฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ด๋Ÿฐ ๊ตฌ์กฐ์ฒด๋Š” ์œ ๋‹› ํƒ€์ž…๊ณผ ๋น„์Šทํ•˜๋‹ค.
  • ๋ฐ์ดํ„ฐํ•„๋“œ๊ฐ€ ์—†๋Š” trait๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ํƒ€์ž… ํŒŒ๋ผ๋ฏธํ„ฐ๋‚˜ ํ…Œ์ŠคํŠธ์šฉ mock ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

5.2 An Example Program Using Structs

๊ตฌ์กฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋ฅผ ๊ตฌ์กฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฆฌํŒฉํ† ๋งํ•˜๋Š” ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ.

fn main() {
    let width1 = 30;
    let height1 = 50;

    println!(
        "The area of the rectangle is {} square pixels.",
        area(width1, height1)
    );
}
  • ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ๋‘๊ฐœ์ด๋ฉฐ, ํ•ด๋‹น ๊ฐ’๋“ค์ด ์–ด๋–ค ๊ฐ’์ธ์ง€์— ๋Œ€ํ•œ ์ธ์ง€๊ฐ€ ํ•„์š”ํ•ด์„œ ๋ณ„๋กœ๋ผ๊ณ  ํ•œ๋‹ค.

5.2.1 Refactoring with Tuples

fn main() {
    let rect1 = (30, 50);

    println!(
        "The area of the rectangle is {} square pixels.",
        area(rect1)
    );
}

fn area(dimensions: (u32, u32)) -> u32 {
    dimensions.0 * dimensions.1
}
  • ์กฐ๊ธˆ ๋” ๋‚ซ๊ธด ํ•˜์ง€๋งŒ, ์˜๋ฏธ์ ์œผ๋กœ dimensions.0๊ณผ dimensions.1์ด width์™€ height๋ผ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์—†๋‹ค.

5.2.2 Refactoring with Structs: Adding More Meaning

fn main() {
    let rect1 = Rectangle { width: 30, height: 50 };

    println!(
        "The area of the rectangle is {} square pixels.",
        area(&rect1)
    );
}

fn area(rectangle: &Rectangle) -> u32 {
    rectangle.width * rectangle.height
}
  • ๊ตฌ์กฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€๋…์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค.
  • ownership์„ ๋„˜๊ธฐ์ง€ ์•Š๊ณ  ์ฐธ์กฐ๋ฅผ ๋„˜๊ธฐ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

5.2.3 Adding Useful Functionality with Derived Traits

struct Rectangle {
    width: u32,
    height: u32,
}

fn main() {
    let rect1 = Rectangle { width: 30, height: 50 };

    println!("rect1 is {}", rect1);
}
  • println! ๋งคํฌ๋กœ๋Š” Display trait๋ฅผ ๊ตฌํ˜„ํ•œ ํƒ€์ž…๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค, ๊ด€๋ จ๋œ ์ปดํŒŒ์ผ ์—๋Ÿฌ๋ฅผ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.
   = help: the trait `std::fmt::Display` is not implemented for `Rectangle`
   = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
  • ์นœ์ ˆํ•œ ์ปดํŒŒ์ผ๋Ÿฌ์˜ ์กฐ์–ธ์„ ๋”ฐ๋ผ {:?}๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ถœ๋ ฅํ•˜๋Š” ์˜ˆ์ œ.
fn main() {
    let rect1 = Rectangle { width: 30, height: 50 };

    println!("rect1 is {:?}", rect1);
}
  • ๋˜ ๋‹ค๋ฅธ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
   = note: `Rectangle` cannot be formatted using `{:?}` because it doesn't implement `std::fmt::Debug`
  • Debug trait๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค๋Š” ์—๋Ÿฌ์ด๋‹ค.
  • #[derive(Debug)]๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Debug trait๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ด๋Š” #[derive(Debug)]๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ž๋™์œผ๋กœ ๊ตฌํ˜„ํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค.
#[derive(Debug)]
struct Rectangle {
    width: u32,
    height: u32,
}
  • ์ด์ œ {:?}๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค.
  • {:?}๋Š” Debug trait๋ฅผ ๊ตฌํ˜„ํ•œ ํƒ€์ž…์„ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค.
fn main() {
    let rect1 = Rectangle { width: 30, height: 50 };

    println!("rect1 is {:?}", rect1);
}
  • Debug format์„ ์‚ฌ์šฉํ•ด์„œ ์ถœ๋ ฅํ•˜๋Š” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์€ dbg! ๋งคํฌ๋กœ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
  • dbg! ๋งคํฌ๋กœ๋Š” println! ๋งคํฌ๋กœ์™€ ๋น„์Šทํ•˜์ง€๋งŒ, dbg! ๋งคํฌ๋กœ๋Š” ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ownership์„ ๊ฐ€์ ธ๊ฐ”๋‹ค ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค (println!์€ ์ฐธ์กฐ๋งŒ ๊ฐ€์ ธ๊ฐ„๋‹ค.)
#[derive(Debug)]
struct Rectangle {
    width: u32,
    height: u32,
}

fn main() {
    let scale = 2;
    let rect1 = Rectangle {
        width: dbg!(30 * scale),
        height: 50,
    };

    dbg!(&rect1);
}
$ cargo run
   Compiling rectangles v0.1.0 (file:///projects/rectangles)
    Finished dev [unoptimized + debuginfo] target(s) in 0.61s
     Running `target/debug/rectangles`
[src/main.rs:10] 30 * scale = 60
[src/main.rs:14] &rect1 = Rectangle {
    width: 60,
    height: 50,
}

5.3 Method Syntax

  • ๊ตฌ์กฐ์ฒด์— ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋ฉ”์„œ๋“œ๋Š” ํ•จ์ˆ˜์™€ ๋น„์Šทํ•˜์ง€๋งŒ, ๊ตฌ์กฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค์— ๋Œ€ํ•ด ํ˜ธ์ถœ๋œ๋‹ค๋Š” ์ ์ด ๋‹ค๋ฅด๋‹ค.
  • ๋ฉ”์„œ๋“œ๋Š” self ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค.
  • self ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

5.3.1 Defining Methods

#[derive(Debug)]
struct Rectangle {
    width: u32,
    height: u32,
}

impl Rectangle {
    fn area(&self) -> u32 {
        self.width * self.height
    }
}

fn main() {
    let rect1 = Rectangle {
        width: 30,
        height: 50,
    };

    println!(
        "The area of the rectangle is {} square pixels.",
        rect1.area()
    );
}
  • impl ๋ธ”๋ก์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • impl ๋ธ”๋ก์€ ๊ตฌ์กฐ์ฒด์˜ ์ด๋ฆ„๊ณผ ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•  ๊ตฌ์กฐ์ฒด์˜ ์ด๋ฆ„์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

  • self ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

  • self ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” self: &self์˜ ์•ฝ์–ด์ด๋‹ค.

  • Self ํƒ€์ž…์€ impl ๋ธ”๋ก์ด ์ ์šฉ๋˜๋Š” ํƒ€์ž…์˜ ๋ณ„์นญ์ด๋‹ค. (alias)

  • rectangle: &Rectangle ์—์„œ์ฒ˜๋Ÿผ, ์ด ๋ฉ”์†Œ๋“œ๊ฐ€ Self ์ธ์Šคํ„ด์Šค๋ฅผ ๋นŒ๋ฆฐ๋‹ค๋Š” ๊ฒƒ์„ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด self ์•ฝ์–ด ์•ž์— &๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

  • ๋ฉ”์†Œ๋“œ๋Š” ๋‹ค๋ฅธ ๋งค๊ฐœ๋ณ€์ˆ˜์ฒ˜๋Ÿผ self์˜ ์†Œ์œ ๊ถŒ์„ ๊ฐ€์งˆ ์ˆ˜๋„ ์žˆ๊ณ , ์—ฌ๊ธฐ์ฒ˜๋Ÿผ self๋ฅผ ๋ถˆ๋ณ€์œผ๋กœ ๋นŒ๋ฆด ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ, ํ˜น์€ self๋ฅผ ๊ฐ€๋ณ€์œผ๋กœ ๋นŒ๋ฆด ์ˆ˜๋„ ์žˆ๋‹ค.

  • ๋ฐ˜๋Œ€๋กœ &mut self๋กœ ์„ ์–ธํ•˜๋ฉด, ํ•ด๋‹น ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ณ€์œผ๋กœ ๋นŒ๋ฆด ์ˆ˜ ์žˆ๋‹ค.

  • ํŠน์ •ํ•œ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ณ€ํ™”์‹œํ‚ค๊ณ , ๊ธฐ์กด์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ฒŒ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, &mut self๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

struct Circle {
    radius: f64,
}

impl Circle {
    // Circle์˜ ๋ฉด์ ์„ ๊ณ„์‚ฐํ•˜๋Š” ๋ฉ”์„œ๋“œ
    fn area(&self) -> f64 {
        3.14159 * self.radius * self.radius
    }

    // Circle์˜ ๋ฐ˜์ง€๋ฆ„์„ ์ฃผ์–ด์ง„ ๋ฐฐ์œจ๋กœ ์กฐ์ •ํ•˜๋Š” ๋ฉ”์„œ๋“œ
    fn scale(&mut self, factor: f64) {
        self.radius *= factor;
    }
}

fn main() {
    let mut circle = Circle { radius: 5.0 };

    println!("์›๋ž˜ ๋ฉด์ : {}", circle.area());

    // circle์˜ ๋ฐ˜์ง€๋ฆ„์„ 2๋ฐฐ๋กœ ์กฐ์ •
    circle.scale(2.0);

    println!("์กฐ์ •๋œ ๋ฉด์ : {}", circle.area());
}
impl Circle {
    // Circle์„ Square๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ
    fn into_square(self) -> Square {
        Square { side: self.radius * 2.0 }
    }
}

struct Square {
    side: f64,
}

fn main() {
    let circle = Circle { radius: 5.0 };
    let square = circle.into_square(); // ์—ฌ๊ธฐ์„œ circle์˜ ์†Œ์œ ๊ถŒ์ด ์ด๋™๋จ

    // println!("์›์˜ ๋ฐ˜์ง€๋ฆ„: {}", circle.radius); // ์˜ค๋ฅ˜: `circle`์€ ๋” ์ด์ƒ ์œ ํšจํ•˜์ง€ ์•Š์Œ
    println!("์ •์‚ฌ๊ฐํ˜•์˜ ๋ณ€ ๊ธธ์ด: {}", square.side);
}
fn main() {
    let circle = Circle { radius: 5.0 };

    // circle.scale(2.0); // ์˜ค๋ฅ˜: `circle`์€ ๋ถˆ๋ณ€ ์ฐธ์กฐ์ด๋ฉฐ, `scale`์€ ๊ฐ€๋ณ€ ์ฐธ์กฐ๋ฅผ ์š”๊ตฌํ•จ
}
  • ํ•จ์ˆ˜ ๋Œ€์‹  ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” ๋‹จ์ง€ ๊ตฌ์กฐ์ฒด์—์„œ ํƒ€์ž…์„ ๋งค๋ฒˆ ์“ฐ์ง€ ์•Š๊ธฐ ์œ„ํ•ด์„œ๊ฐ€ ์•„๋‹ˆ๋ผ, ํ•ด๋‹น ํƒ€์ž…์— ๋Œ€ํ•œ method๋ฅผ ์กฐ์งํ™” ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. (organization)

  • ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ํ•ด๋‹น ํƒ€์ž…์— ๋Œ€ํ•œ ๋ชจ๋“  ๊ธฐ๋Šฅ์„ ํ•œ ๊ณณ์— ๋ชจ์•„๋‘˜ ์ˆ˜ ์žˆ๋‹ค.

  • ํ•„๋“œ์˜ ์ด๋ฆ„๊ณผ ๋ฉ”์„œ๋“œ์˜ ์ด๋ฆ„์ด ๊ฐ™๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • getter์™€ ๊ฐ™์€๊ฒƒ๋“ค๋„ ๊ตฌํ˜„ํ•˜๋Š”๋ฐ, ์ด๊ฑด ๋‚˜์ค‘์—..

5.3.2 Where’s the -> Operator?

  • (c/cpp ์—์„œ) -> ์—ฐ์‚ฐ์ž๋Š” ํฌ์ธํ„ฐ์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ๊ฐ์ฒด์—์„œ ์ง์ ‘ ํ˜ธ์ถœํ•˜๋Š” ๊ฒฝ์šฐ๋Š” .
  • ์—ญ์ฐธ์กฐ๋ฅผ ํ•ด์„œ ํ˜ธ์ถœํ•  ํ•„์š”๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ธ๋ฐ, object ๊ฐ€ ํฌ์ธํ„ฐ๋ผ๋ฉด, object->method()๋Š” (*object).method()์™€ ๋น„์Šทํ•˜๋‹ค.
  • ๋Ÿฌ์ŠคํŠธ์—์„œ๋Š” ์ด๋Ÿฌํ•œ ๊ณผ์ •์ด ์ž๋™ํ™” ๋˜์–ด์žˆ๋‹ค (automatic referencing and dereferencing).
  • object.method() ์™€ ๊ฐ™์ด ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด, ๋Ÿฌ์ŠคํŠธ๋Š” ์ž๋™์œผ๋กœ &, &mut, *๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ํ˜ธ์ถœํ•œ๋‹ค.
// same
p1.distance(&p2);
(&p1).distance(&p2);
  • ์ง์ž‘ํ• ์ˆ˜ ์žˆ๋“ฏ์ด self๋ผ๋Š” ๋ช…ํ™•ํ•œ ์ˆ˜์‹ ์ž๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€๋Šฅํ•˜๋‹ค

  • ๋ฆฌ์‹œ๋ฒ„๊ฐ€ &self, &mut self, self๋กœ ์ •์˜๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ์ฝ๊ธฐ ์ˆ˜์ • ์†Œ๋น„์— ๋Œ€ํ•œ ๋ถ€๋ถ„์„ ๋ช…ํ™•ํ•˜๊ฒŒ ํŒŒ์•… ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ์˜ˆ๋ฅผ ๋“ค์–ด, ์–ด๋–ค ๊ฐ์ฒด obj๊ฐ€ ์žˆ๊ณ  ์ด ๊ฐ์ฒด์— ๋Œ€ํ•œ ๋ฉ”์„œ๋“œ method()๊ฐ€ ์ •์˜๋˜์–ด ์žˆ์„ ๋•Œ,

  • Rust๋Š” obj.method() ํ˜ธ์ถœ์„ ์ ์ ˆํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•œ๋‹ค.

  • ์ด ๋ฉ”์„œ๋“œ๊ฐ€ &self๋ฅผ ์š”๊ตฌํ•œ๋‹ค๋ฉด, Rust๋Š” ์ž๋™์œผ๋กœ &obj.method()๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

  • ๋งŒ์•ฝ ๋ฉ”์„œ๋“œ๊ฐ€ &mut self๋ฅผ ์š”๊ตฌํ•œ๋‹ค๋ฉด, Rust๋Š” &mut obj.method()๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค.

  • ๊ฐ์ฒด๊ฐ€ ๊ฐ’์œผ๋กœ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค๋ฉด, Rust๋Š” ํ•„์š”์— ๋”ฐ๋ผ (*obj).method()์ฒ˜๋Ÿผ ์—ญ์ฐธ์กฐ๋ฅผ ์ž๋™์œผ๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค.

5.3.3 Methods with More Parameters

  • ๋ฉ”์„œ๋“œ๋Š” ์ถ”๊ฐ€์ ์ธ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.
fn main() {
    let rect1 = Rectangle {
        width: 30,
        height: 50,
    };
    let rect2 = Rectangle {
        width: 10,
        height: 40,
    };
    let rect3 = Rectangle {
        width: 60,
        height: 45,
    };

    println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2));
    println!("Can rect1 hold rect3? {}", rect1.can_hold(&rect3));
}
impl Rectangle {
    fn can_hold(&self, other: &Rectangle) -> bool {
        self.width > other.width && self.height > other.height
    }
}
  • self reciever ์ดํ›„๋Š” ๊ทธ๋ƒฅ ์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜์™€ ๋™์ผํ•˜๋‹ค.

5.3.4 Associated Functions

  • self ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ฐ€์ง€์ง€ ์•Š๋Š” ํ•จ์ˆ˜๋ฅผ impl ๋ธ”๋ก ๋‚ด์— ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • self ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์—, ๋ฉ”์„œ๋“œ๋Š” ์•„๋‹ˆ๊ณ  associated function์ด๋ผ๊ณ  ํ•œ๋‹ค.

  • associated function์€ ์ฃผ๋กœ ๊ตฌ์กฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.

impl Rectangle {
    fn square(size: u32) -> Self {
        Self {
            width: size,
            height: size,
        }
    }
}
  • ๋งˆ์ง€๋ง‰์œผ๋กœ impl๋ธ”๋ก์„ ์—ฌ๋Ÿฌ๊ฐœ ๋‘˜ ์ˆ˜ ์žˆ๋Š”๋ฐ, ํฐ ์˜๋ฏธ๋Š” ์—†๋Š” ๊ฒƒ ๊ฐ™๋‹ค