Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR RUST

rust struct default values

struct Point {
    x: i32,
    y: i32
}

impl Default for Point {
    fn default() -> Point {
        Point {
            x: 0,
            y: 1
        }
    }
}

fn main() {
    // initialize a default point
    let p1 = Point { ..Default::default() };

    // initialize a point with default value for all fields except 
    // y that is set to 0
    let p2 = Point { y: 0, ..Default::default() };
    
    println!("{}, {}", p1.x, p1.y); // 0, 1
    println!("{}, {}", p2.x, p2.y); // 0, 0
	
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #rust #struct #default #values
ADD COMMENT
Topic
Name
3+8 =