可以克隆std::ops::Generator
类型的变量吗?有一个生成器,我想创建一个新的实例,有一个干净的堆栈,没有clone()
方法。我在Box
和Pin
内部都试过了,但它不能克隆!
#![feature(generators, generator_trait)]
fn main() {
let gen = move || yield ();
let gen1 = gen.clone();
let gen = Box::new(move || yield ());
let gen1 = gen.clone();
let gen = Box::pin(move || yield ());
let gen1 = gen.clone();
}
error[E0599]: no method named `clone` found for type `[generator@src/main.rs:4:15: 4:31 _]` in the current scope
--> src/main.rs:6:20
|
6 | let gen1 = gen.clone();
| ^^^^^
据我所知,闭包是可以克隆的;生成器有什么问题?
我发现的唯一解决办法是从某个函数返回生成器(然后它每次都是新的),但这在语法上对我来说很不方便。
https://stackoverflow.com/questions/56676309
复制相似问题