我已经试了几天了。下面的最小示例显示了我当前问题的状态:
我有一个迭代器,它引用一个外部片。我想从一个按钮中提前(呼叫next())。
Cargo.toml
[dependencies]
cursive = "0.18"main.rs
use cursive::views::Dialog;
use cursive::Cursive;
fn main() {
let slice = [1, 2, 3, 4, 5, 6];
let mut chunks = slice.chunks_exact(2);
let mut siv = cursive::default();
siv.add_layer(
Dialog::text("...")
.button("Quit", |s| s.quit())
.button("Next", |s| next_thing(s, &mut chunks)),
);
siv.run();
}
fn next_thing<'a, I>(_: &mut Cursive, things: &mut I)
where
I: Iterator<Item = &'a [u8]>,
{
// I want to call things.next() and to something with the slice.
}但是闭包不能接受可变的引用。经过多次失败的尝试后,我还发现Cursive有这一功能来存储用户数据,但是我不能将迭代器保存在那里,因为它不是'static。
发布于 2022-06-21 08:58:52
use cursive::views::Dialog;
use cursive::Cursive;
use std::cell::RefCell;
use std::sync::Arc;
const SLICE: [u8; 6] = [1, 2, 3, 4, 5, 6];
fn main() {
let chunks = Arc::new(RefCell::new(SLICE.chunks_exact(2)));
let mut siv = cursive::default();
siv.add_layer(
Dialog::text("...")
.button("Quit", |s| s.quit())
.button("Next", move |s| next_thing(s, &chunks)),
);
siv.run();
}
fn next_thing<'a, I>(_: &mut Cursive, things: &Arc<RefCell<I>>)
where
I: Iterator<Item = &'a [u8]>,
{
eprintln!("{:?}", things.borrow_mut().next());
}https://stackoverflow.com/questions/72694352
复制相似问题