LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 1 year has passed since last update.

#[tokio::main]は何をやっているのか?

Posted at
1 / 14

About me

Rust素人


Rustで非同期処理を書いていたり、非同期処理を扱うライブラリを見ていると必ずと言っていいほどtokioを利用しているケースに遭遇します。
tokioの利用では#[tokio::main]という謎の属性に遭遇しこれは一体何なのでしょう?


Rustのマクロを知る


Rustには2種類のマクロが提供されており、#[xxxx]という属性は手続き的マクロと呼ばれるものです。
これは属性を与えた型から新しいコードを生成するものです。


// 何も処理をしない関数に適当な属性を指定すると…
#[give_hello]
fn empty() {}

give_helloというマクロは存在しないのですが、雰囲気でこういうマクロがあるんだなと捉えてください。


// 以下のようなコードに変換される
fn empty() {
    println!("Hello world!!")
}

ということで、#[tokio::main]を指定した関数は何らかの処理が加えられた別の実装に置き換えられているのです。


どんな風に置き換えられるのか?


tokioのドキュメントにしっかり記述されています。
以下のように書いたコードは…

// tokioのドキュメントから抜粋 
// See: https://docs.rs/tokio/1.10.1/tokio/attr.main.html#using-the-multi-thread-runtime
#[tokio::main]
async fn main() {
    println!("Hello world");
}

以下のように評価されます。

// tokioのドキュメントから抜粋 
// See: https://docs.rs/tokio/1.10.1/tokio/attr.main.html#using-the-multi-thread-runtime
fn main() {
    tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            println!("Hello world");
        })
}

この実装から、#[tokio::main]asyncキーワードをつけた関数を同期関数に変換し、
tokioのランタイムの中で非同期処理として実行させることがわかりました。


参考

Rustのドキュメント
https://doc.rust-jp.rs/book-ja/ch19-06-macros.html

tokioのドキュメント
https://docs.rs/tokio/1.10.1/tokio/attr.main.html


ありがとうございました。

0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up