mirror of
https://github.com/rust-lang/rust.git
synced 2025-11-03 06:25:40 +00:00
This adds an `iter!` macro that can be used to create movable generators. This also adds a yield_expr feature so the `yield` keyword can be used within iter! macro bodies. This was needed because several unstable features each need `yield` expressions, so this allows us to stabilize them separately from any individual feature. Co-authored-by: Oli Scherer <github35764891676564198441@oli-obk.de> Co-authored-by: Jieyou Xu <jieyouxu@outlook.com> Co-authored-by: Travis Cross <tc@traviscross.com>
31 lines
667 B
Rust
31 lines
667 B
Rust
//@ run-pass
|
|
|
|
#![feature(iter_macro, yield_expr)]
|
|
|
|
// This test creates an iterator that captures a reference and ensure that doesn't force the
|
|
// iterator to become lending.
|
|
|
|
use std::iter::iter;
|
|
|
|
fn main() {
|
|
let s = "foo".to_string();
|
|
let f = iter! { || {
|
|
for c in s.chars() {
|
|
yield c;
|
|
}
|
|
}};
|
|
|
|
let mut i = f();
|
|
let mut j = f();
|
|
|
|
assert_eq!(i.next(), Some('f'));
|
|
assert_eq!(i.next(), Some('o'));
|
|
assert_eq!(i.next(), Some('o'));
|
|
assert_eq!(i.next(), None);
|
|
|
|
assert_eq!(j.next(), Some('f'));
|
|
assert_eq!(j.next(), Some('o'));
|
|
assert_eq!(j.next(), Some('o'));
|
|
assert_eq!(j.next(), None);
|
|
}
|