Files
rust/tests/ui/move-expr/nested-closures.rs
T
2026-05-11 16:21:14 +09:00

28 lines
722 B
Rust

//@ check-pass
#![allow(incomplete_features)]
#![feature(move_expr)]
use std::sync::Arc;
fn main() {
let x = Arc::new(String::from("hello"));
assert_eq!(Arc::strong_count(&x), 1);
let outer = || {
assert_eq!(Arc::strong_count(&x), 1);
let inner = || move(x.clone());
assert_eq!(Arc::strong_count(&x), 2);
let y = inner();
assert_eq!(&*y, "hello");
assert_eq!(Arc::strong_count(&x), 2);
drop(y);
assert_eq!(Arc::strong_count(&x), 1);
};
assert_eq!(Arc::strong_count(&x), 1);
// `outer` captures `x` by reference, and `inner` takes ownership of a clone.
println!("{x}");
outer();
assert_eq!(Arc::strong_count(&x), 1);
}