trpl-zh-cn/listings/ch05-using-structs-to-structure-related-data/no-listing-03-associated-functions/src/main.rs

21 lines
294 B
Rust
Raw Normal View History

2022-02-06 16:43:51 +08:00
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
// ANCHOR: here
impl Rectangle {
fn square(size: u32) -> Self {
Self {
2022-02-06 16:43:51 +08:00
width: size,
height: size,
}
}
}
// ANCHOR_END: here
fn main() {
let sq = Rectangle::square(3);
}