Created
December 12, 2018 16:12
-
-
Save rust-play/f4ffc6792d65b59c1da5dc044e357143 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains 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
/// Methods | |
struct Point { x: i32, y: i32 } | |
impl Point { | |
fn from(x: i32, y: i32) -> Point { // Associated function = no self | |
Point { x, y } | |
} | |
fn tostr(&self) -> String { // Method takes self as param | |
format!("Point({}, {})", self.x, self.y) | |
} | |
} | |
fn main() { | |
let pt1 = Point::from(11, 12); | |
println!("val: {}", pt1.tostr()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment