Last active
May 3, 2022 16:49
-
-
Save soyart/1d141943e2d7eaff00c933425124a0b9 to your computer and use it in GitHub Desktop.
My practice code for https://doc.rust-lang.org/book/ch05-03-method-syntax.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #![allow(dead_code)] | |
| use std::fmt; | |
| // Tuple struct | |
| struct RGBColor(u8, u8, u8); | |
| // Manually implementing std::fmt::Debug | |
| impl fmt::Debug for RGBColor { | |
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
| f.debug_struct("RGBColor") // Must be exactly "RGBColor" in this case | |
| .field("red", &self.0) | |
| .field("green", &self.1) | |
| .field("blue", &self.2) | |
| .finish() | |
| } | |
| } | |
| // Manually implementing Default (for default value) | |
| impl Default for RGBColor { | |
| fn default() -> RGBColor{ | |
| RGBColor(0, 0, 0) | |
| } | |
| } | |
| struct Rectangle { | |
| w: u64, | |
| h: u64, | |
| } | |
| // Define methods for Rectangle | |
| impl Rectangle { | |
| fn area(&self) -> u64 { | |
| self.w * self.h | |
| } | |
| fn larger_than(&self, other: &Rectangle) -> bool { | |
| self.area() > other.area() | |
| } | |
| } | |
| // Multiple impl block is possible | |
| impl Rectangle { | |
| // square doesn't take in &self, | |
| // call it with: Rectangle::square(69) | |
| fn square(side: u64) -> Rectangle { | |
| Rectangle { w: side, h: side } | |
| } | |
| } | |
| // Named struct, which implements std::fmt::Debug | |
| // by simply using derive attribute. | |
| #[derive(Debug)] | |
| struct User { | |
| username: String, | |
| email: String, | |
| sign_in_count: u64, | |
| active: bool, | |
| fav_color: RGBColor, | |
| } | |
| fn main() { | |
| let black = RGBColor(0, 0, 0); | |
| let red = RGBColor(255, 0, 0); | |
| println!("black is {:?}", black); | |
| println!("red is {:?}", red); | |
| let b = new_user(String::from("[email protected]"), String::from("my_b"), red); | |
| println!("b {:?}", b); | |
| // Local tuple struct | |
| struct Point(i32, i32, i32); | |
| let _p = Point(69, 23, 27); | |
| // Can't call standalone_area with p - invalid type, although | |
| // they both are actually (i32, i32, i32) | |
| // println!("standalone_area of point {:?}, {}", p, standalone_area(p)) | |
| let rect = Rectangle { w: 69, h: 96 }; | |
| // standalone_area borrows rect without moving | |
| println!("area {}", standalone_area(&rect)); | |
| // so we can still use rect later | |
| println!("rect.w: {}, rect.h: {}", rect.w, rect.h); | |
| println!("rect.area() -> {}", rect.area()); | |
| // Calling non-associative method | |
| let _square = Rectangle::square(69); | |
| let larger_rect = Rectangle { w: 96, h: 96 }; | |
| println!("larger_rect.area(): {}", larger_rect.area()); | |
| println!("larger_rect > rect: {}", larger_rect.larger_than(&rect)); | |
| } | |
| fn new_user(email: String, username: String, color: RGBColor) -> User { | |
| User { | |
| // Syntactic sugar | |
| email, | |
| username, | |
| active: true, | |
| sign_in_count: 0, | |
| fav_color: color, | |
| } | |
| } | |
| fn standalone_area(r: &Rectangle) -> u64 { | |
| r.w * r.h | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment