Created
July 3, 2020 16:54
-
-
Save yongkyuns/e74aac9b28edd98394510a7383a5339c to your computer and use it in GitHub Desktop.
Rust Default
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
#[derive(Debug)] | |
struct Point { | |
x: f32, | |
y: f32, | |
} | |
impl Point { | |
fn new() -> Self { | |
Point { x: 30.0, y: 5.0 } | |
// Default::default() // This also works | |
} | |
} | |
impl Default for Point { | |
fn default() -> Self { | |
Point { | |
x: 5.0, | |
y: Default::default(), | |
} | |
} | |
} | |
fn main() { | |
let p = Point { x: 10.0, y: 20.0 }; | |
let p2 = Point::new(); | |
let p3: Point = Default::default(); | |
println!("p = {:?}", p); | |
println!("p2 = {:?}", p2); | |
println!("p4 = {:?}", p3); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment