This maybe because I'm used to backend and async in js but if I type this in rust..
fs::create_dir("dir1")
Then use
fs::File::create("dir1/file1.txt")
To create a file in the new folder right after, is it bound to go wrong, should the program sleep for a second to make sure the directory has already been created on the environment?
To answer this, No it is not bound to go wrong. And also to add in general, sleeping to ensure that something is being done is not that of a good idea.
In the case you provided, fs::create_dir
is sync and it returns a Result type. So before you create file inside dir1
make sure you check the result is ok. And also check the result for creating file is ok.
fn create_file(){
if fs::create_dir("dir1").is_ok() {
if fs::File::create("dir1/file1.txt").is_ok() {
println!("File created inside dir1");
}
}
}
Also, while inside rust you can take the gurantee that once directory is created it stays there but this won't always be case. For example, a system might have a background job that keeps deleting the directory inside some place. Hopefully, you don't always have to worry about all these extreme cases but just so you know ;)