Skip to content

Instantly share code, notes, and snippets.

@hsnks100
Created October 5, 2021 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hsnks100/f24b299e8c4122dc1305b14ba4bf1a9a to your computer and use it in GitHub Desktop.
Save hsnks100/f24b299e8c4122dc1305b14ba4bf1a9a to your computer and use it in GitHub Desktop.
rust 구조체 개념박기
fn main() {
println!("area: {}", area(10, 10));
println!("area2: {}", area2((10, 10)));
let rect = Rectangle{h:10, w:10};
println!("area3: {}", area3(&rect));
println!("area3-1: {:?}", rect);
println!("area4: {:?}", rect.area());
} // 여기서 x는 스코프 밖으로 나가고, s도 그 후 나갑니다. 하지만 s는 이미 이동되었으므로,
// 별다른 일이 발생하지 않습니다.
#[derive(Debug)]
struct Rectangle {
h: i32,
w: i32,
}
impl Rectangle {
fn area(&self) -> i32 {
self.h * self.w
}
}
fn area(w: i32, h: i32) -> i32{
w * h
}
fn area2(d:(i32, i32)) -> i32{
d.0 * d.1
}
fn area3(d: &Rectangle) -> i32{
d.h * d.w
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment