1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rustでstr.lines()を使う ~ イテレータとenumerate() ~

Last updated at Posted at 2022-02-26
fn main() {
  let quote = "\
The quick brown fox jumps over
the lazy dog.";
  for (i, line) in quote.lines().enumerate() {
    let line_num = i + 1;
    println!("{}: {}", line_num, line);
  }
}

Rustではイテレータを利用する際に生成する必要がある。

文字列の場合linesがイテレータを生成している。
Lines()はString型を文字列スライスとして扱うイテレータ。

また、for in でインデックスを取るためにenumrate()を使う構文はPythonにも存在している。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?