Skip to content

Instantly share code, notes, and snippets.

@yongkyuns
Created July 3, 2020 16:54
Show Gist options
  • Save yongkyuns/e74aac9b28edd98394510a7383a5339c to your computer and use it in GitHub Desktop.
Save yongkyuns/e74aac9b28edd98394510a7383a5339c to your computer and use it in GitHub Desktop.
Rust Default
#[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