0
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 1 year has passed since last update.

同じ要素をn回繰り返すイテレータ・文字列を作成したい

0
Posted at

同じ要素をn回繰り返すイテレータを作成したい場合、std::iter::repeatを利用します。

let mut iter = std::iter::repeat('A').take(3);
assert_eq!(iter.next(), Some('A'));
assert_eq!(iter.next(), Some('A'));
assert_eq!(iter.next(), Some('A'));
assert_eq!(iter.next(), None);

同じ要素をn回繰り返す文字列については、std::iter::repeatを利用する必要はなく、repeatメソッドを利用すればよいです。

let s = "ABC".repeat(3);
assert_eq!(s, "ABCABCABC");

環境情報

$ rustc -V
rustc 1.75.0 (82e1608df 2023-12-21)
0
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
0
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?