How to have struct var initialized as nothing?

I want to have my_value in impl A initialized to nothing; no 0.0, and no Option. Is there a way to do that?

pub struct A {
        pub my_value: f64,
}
impl A {
      pub fn new() -> A {
        A {
            my_value: (), // HERE
        }
    }
}

pub struct X {
    pub my_number: A,
}

impl X {
    pub fn new() -> X {
        X {
            my_number: A::new(),
        }
    }
}

fn main() {
    let mut x = X::new(); 
    x.my_number.my_value = 9.9;
    println!("{:?}", x.my_number.my_value);
}

What’s your goal with this?

You can use something like MaybeUninit which requires unsafe but I have a feeling that may not be what you want.

Another option may be to use the builder pattern Link.

A final option which I’m not sure how it interacts with nested structs is to just predeclare the variable with let mut x;

What's the point?

@drewkett @chenhengqi I am exploring this due to troubles I'm having with Option. I'll do a new post with my Option problem.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.