-1

The following piece of code is taken from the tokio crate tutorial

use tokio::io::{self, AsyncWriteExt};
use tokio::fs::File;

#[tokio::main]
async fn main() -> io::Result<()> {
    let mut file = File::create("foo.txt").await?;

    // Writes some prefix of the byte string, but not necessarily all of it.
    let n = file.write(b"some bytes").await?;

    println!("Wrote the first {} bytes of 'some bytes'.", n);
    Ok(())
}

Why do we need to .await when creating a file?

let mut file = File::create("foo.txt").await?;

In other words, why do we need to create a file asynchronously? After all, we can't write to a file if it is not created yet, and hence it's enough simply to block on creating. If it creates successfully then write to it asynchronously, otherwise simply return an error. I definitely miss something.

UPDATE: Please don't try to long-explain what asynchronous programming is, or what .await does. I know these topics very well. My question is: what is the reason in this example of creating a file asynchronously?

storm
  • 795
  • 1
  • 5
  • 12
  • 3
    "we can't write to a file if it is not created yet" - that is precisely the point of `.await` - pause executing the current function (and allow the executor to run other functions) until the file has been created. You could of course block on file creation, but then other async code would not be able to run while the current one is blocked. – user4815162342 Oct 28 '21 at 19:58
  • @user4815162342 I guess the asynchronous creating is meaningless in this particular example. But if we created another asynchronous task besides create-and-then-writing to a file, then it'd make sense to create the file in async way. Am I on the right track? – storm Oct 28 '21 at 20:03
  • 1
    Examples are meant to show something, they do not need to make sense or be practical at all. Cudos if they are though. – Netwave Oct 28 '21 at 20:32
  • @Netwave If examples do not make sense, then they won't be able to show something. Will simply confuse. Such examples should contain at least a single line saying that it is simply for demo purpose. I hope you understand such simple things. – storm Oct 28 '21 at 20:35
  • @HardFork, sometimes is good to use common sense too. – Netwave Oct 28 '21 at 21:18
  • 1
    *But if we created another asynchronous task besides create-and-then-writing to a file, then it'd make sense to create the file in async way.* - **Correct.** Async examples are assumed to run inside an event loop with many such pieces advancing independently of each other (except when connected through await). – user4815162342 Oct 28 '21 at 21:40
  • 2
    @HardFork I can't really follow your argument. Why do you think the file creation should be synchronous, while writing to the file should be async? Can't you apply exactly the same reasoning to writing to the file? With just a single task, there simply isn't any reason for concurrency, since we only wan to do a single thing at any given time anyway. – Sven Marnach Oct 28 '21 at 22:33

2 Answers2

4

There is no practical reason to use .await in this simplistic example. One could argue this is a bad example, as it does not show any performance improvement over normal synchronous programming. However, a practical example of async is typically more complex, and the purpose of this example is to introduce the basic syntax.

effect
  • 1,279
  • 6
  • 13
1

Working with the file system is an asynchronous task. You are requesting to either write or read from the operating system which works independently of your program. your program can do other stuff while the file is loading. Await basically tells your program to stop execution of this function until the data requested is ready.

Brandon Piña
  • 614
  • 1
  • 6
  • 20