首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用草文件增加带有按钮的迭代器

如何使用草文件增加带有按钮的迭代器
EN

Stack Overflow用户
提问于 2022-06-21 01:09:01
回答 1查看 55关注 0票数 1

我已经试了几天了。下面的最小示例显示了我当前问题的状态:

我有一个迭代器,它引用一个外部片。我想从一个按钮中提前(呼叫next())。

Cargo.toml

代码语言:javascript
复制
[dependencies]
cursive = "0.18"

main.rs

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

发布于 2022-06-21 08:58:52

代码语言:javascript
复制
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());
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72694352

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档