/
ZVasilii
/
RustBookLearning
Обзор
Документация
Войти
/
ZVasilii
/
RustBookLearning
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
rectangle_struct/src/main.rs
51 строка
1 KB
vlzaytsev
Upload files
01 окт 2024, 23:22
01 окт 2024, 23:22
c2258e4
Код
Авторство
О чём код?
#[derive(Debug)] struct Rectangle { width: u32, height: u32, } impl Rectangle { fn area(&self) -> u32 { self.width * self.height } fn is_fit_into(&self, other: &Rectangle) -> bool { self.width < other.width && self.height < other.height } fn width(&self) -> u32 { self.width } fn height(&self) -> u32 { self.height } fn get_square(width: u32) -> Self { Rectangle { width, height: width, } } } fn get_rectangle(width: u32, height: u32) -> Rectangle { let tmp = Rectangle { width, height }; tmp } fn main() { let rect1 = Rectangle { width: 5, height: 10, }; let rect2 = get_rectangle(15, 20); println!("Rectangle1 = {:?}", rect1); println!("Rectangle2 = {:#?}", rect2); println!("Rectangle1.area = {}", rect1.area()); println!("Rectangle1.width = {}", rect1.width()); println!("Rectangle1.height = {}", rect1.height()); println!("Rectangle1 fits into 2 = {}", rect1.is_fit_into(&rect2)); println!("Square = {:#?}", Rectangle::get_square(25)); }