I'm learning - File field for later file opening

Hi all.

I think I'm currently dealing with the fact that Rust doesn't let me to explicitly define the absence of value, like null in C or None in Python. Please correct me if I'm wrong.

I want to have a file File field inside a struct:

use std::fs::File;

struct MyStruct {
    file: File,
    other_thing: i32
}

I also want to instantiate this struct and start using the other_thing field and only later open the file when its name becomes available due to the application logic.

What's the proper (rusty?) way to do this?

Thanks a lot.

The Option type is for handling potentially missing data. And you can use a PathBuf or Path if you want to delay opening a file. Of course, you could do Option<File> if you want the file to stay open and accessible.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.