Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【備忘録】Rustのgen記法について

Last updated at Posted at 2025-01-28

gen記法

実は、Rustの2024 editionからgen記法がRustで使えるようになります。

yieldは、関数を一時的に実行停止させることが出来る機能を持つ文で、gen記法と組み合わせることで、イテレータを作ることができます。

python yieldで調べると、わかりやすい説明が出てくるでしょう。

Rustの説明はこのとおりです。

書き方はこんなかんじ。

Rust
gen {
    loop {
        yield 0;
        yield 1;
    }
}
Python
def gen():
    while True:
        yield 0
        yield 1
2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?