Skip to content
Aha00a AhaWiki
  • Search S
  • Random R
  • Raw
  • History H
  • Blame B
  • Login
  • Search
  • Random
  • Login
  • Raw
  • History
  • Blame
created by Aha00aAha00a at 2021-01-02
last modified by Aha00aAha00a at 2021-01-02
revision: 2

The Rust Programming Language - 5. Using Structs to Structure Related Data

  • Defining and Instantiating Structs
  • An Example Program Using Structs
  • Method Syntax
  • See Also
    • Backlinks
    • Adjacent Pages

The Rust Programming Language - 5. Using Structs to Structure Related Data

1. Defining and Instantiating Structs

struct User {
    username: String,
    email: String,
    sign_in_count: u64,
    active: bool,
}
  • 필드 구분자가 comma(,)라는데 유의.
  • trailing comma도 유효
let user1 = User {
    email: String::from("someone@example.com"),
    username: String::from("someusername123"),
    active: true,
    sign_in_count: 1,
};
user1.email = String::from("someone-else@example.com");
  • 순서는 상관없다.
  • member access는 . 으로.
fn build_user(email: String, username: String) -> User {
    User {
        email: email,
        username: username,
        active: true,
        sign_in_count: 1,
    }
}

fn build_user(email: String, username: String) -> User {
    User {
        email,
        username,
        active: true,
        sign_in_count: 1,
    }
}
  • shorthand도 가능.
let user2 = User {
    email: String::from("another@example.com"),
    username: String::from("anotherusername567"),
    active: user1.active,
    sign_in_count: user1.sign_in_count,
};

let user2 = User {
    email: String::from("another@example.com"),
    username: String::from("anotherusername567"),
    ..user1
};
  • Struct update(spread) 가능
    struct Color(i32, i32, i32);
    struct Point(i32, i32, i32);

    let black = Color(0, 0, 0);
    let origin = Point(0, 0, 0);
  • Tuple struct

JavaScript object literal과 상당히 유사함을 알 수 있다.

2. An Example Program Using Structs

그냥 쭉 읽어보면 됨.. Derived Traits 라는게 있다.

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

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

    println!("{:?}", rect1);
    println!("{:#?}", rect1);
}
Rectangle { length: 50, width: 30 }
Rectangle {
    length: 50,
    width: 30
}

3. Method Syntax

  • Method는 첫번째 인자로 &self를 받는다는 점에서 약간 Perl, Python과 비슷하다.
  • Associated Functions(static method)를 만들 수 있고, 호출시 ::를 이용한다는 점은 C++과 비슷하다.
  • ko에는 없고 en에만 있는 내용인데 impl 블럭을 여러개로 나눠서 쓸 수도 있다.
#[derive(Debug)]
struct Rectangle {
    length: u32,
    width: u32,
}


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

impl Rectangle {
    fn can_hold(&self, other: &Rectangle) -> bool {
        self.width > other.width && self.height > other.height
    }
}

impl Rectangle {
    fn square(size: u32) -> Rectangle {
        Rectangle { length: size, width: size }
    }
}

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

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

    let rect1 = Rectangle { length: 50, width: 30 };
    let rect2 = Rectangle { length: 40, width: 10 };
    let rect3 = Rectangle { length: 45, width: 60 };

    println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2));
    println!("Can rect1 hold rect3? {}", rect1.can_hold(&rect3));

    let sq = Rectangle::square(3);
}

4. See Also

4.1. Backlinks

  • The Rust Programming Language(5. Using Structs to Structure Related Data)

4.2. Adjacent Pages

≤ 32
all
1.0x
1.0x
80
-120
ON
0/0
0/0
0.00
0
0 / 0
1.00x

aha00a.com

Aha00a, AhaWiki, Restaurants, Boycott, Donation, Schema

RecentChanges, TitleIndex, PageList, PageMap, WikiStatistics, Schema

Uptime: 2026-11-15T10:05:26.523Z

Best viewed with some ALCOHOL. Copyright© 2001-2026 by Aha00a. All rights reserved. Powered by AhaWiki