Files
rust/tests/ui/iterators/generator_capture_.rs
Oli Scherer 5fbdfc3e10 Add iter macro
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>
2025-06-03 10:52:32 -07:00

27 lines
691 B
Rust

// This test exercises lending behavior for iterator closures which is not yet supported.
#![feature(iter_macro, yield_expr)]
use std::iter::iter;
fn main() {
let f = {
let s = "foo".to_string();
iter! { move || {
for c in s.chars() {
yield c;
}
}}
};
let mut i = f();
assert_eq!(i.next(), Some('f'));
assert_eq!(i.next(), Some('o'));
assert_eq!(i.next(), Some('o'));
assert_eq!(i.next(), None);
let mut i = f(); //~ ERROR use of moved value: `f`
assert_eq!(i.next(), Some('f'));
assert_eq!(i.next(), Some('o'));
assert_eq!(i.next(), Some('o'));
assert_eq!(i.next(), None);
}