struct User {
username: String,
email: String,
sign_in_count: u64,
active: bool,
}
let user1 = User {
email: String::from("Someone@example.com"),
username: String::from("someusername123"),
active: true,
sign_in_count: 1,
};
let user2 = User {
email: String::from("another@example.com"),
username: String::from("anotherusername567"),
..user1
};
struct Color(i32, i32, i32);
struct Point(i32, i32, i32);
let black = Color(0, 0, 0);
let origin = Point(0, 0, 0);
다른 튜플 구조체이기 때문에, black과 origin이 다른 타입.
#[derive(Debug)]
struct Rectangle {
length: u32,
width: u32,
}
fn main() {
let rect1 = Rectangle { length: 50, width: 30 };
println!("rect1 is {:#?}", rect1);
}
다음 코드에서 rect1을 출력하려면 {}으로는 불가능하지만, {:?}, {:#?}으로는 가능하다.
그러기 위해서는 #[derive(Debug)]과 같은 어노테이션을 추가해주어야 한다.
struct Rectangle {
length: u32,
width: u32,
}
impl Rectangle {
fn area (&self) -> u32 {
self.length * self.width
}
}
fn can_hold(&self, rect: &Rectangle) -> bool {
(self.length >= rect.length) && (self.width >= rect.width)
}
rect: &Rectangle 원본 값을 잃지 않지만 &rect: Rectangle 사용할 경우 원본값을 잃음.