私は本当にこれをどうやって得るのか分かりません。私がそれを理解する限り、words
はクロージャーに移動されます(これは私にとっては唯一の場所ですが、この後に使用される唯一の場所ですが)typed_some
に従って& mutである必要があります。エラーが示唆しているのはまともなアイデアのようですが、その部分が図書館にあり、実装できるかどうかわかりません。
on_edit
documentation.閉鎖の中での可変性の問題
extern crate cursive;
extern crate rand;
use cursive::Cursive;
use cursive::views::{Dialog, TextView, EditView, LinearLayout};
use cursive::traits::Identifiable;
use rand::Rng;
fn main() {
// This really messes with stdout. Seems to disable it by default but when
// siv is running println prints in random places on the screen.
let mut siv = Cursive::new();
siv.add_global_callback('q', |s| s.quit());
let mut words = WordBar::new();
siv.add_layer(Dialog::around(LinearLayout::vertical()
.child(TextView::new(words.update_and_get_bar()).with_id("target_field"))
.child(EditView::new()
.on_edit(move |s, input, _| words.typed_some(s, input))
.with_id("input_field")))
.title("Keyurses")
.button("Quit", |s| s.quit()));
siv.run();
}
type WordList = Vec<&'static str>;
#[derive(Debug)]
struct WordBar {
words: WordList,
target_list: WordList,
}
impl WordBar {
fn new() -> Self {
WordBar {
words: include_str!("google-10000-english-usa.txt").lines().collect(),
target_list: vec!["foo"],
}
}
fn typed_some(&mut self, siv: &mut Cursive, input: &str) {
// See https://github.com/gyscos/Cursive/issues/102
// for discussion on this mess
let mut reset_input = false;
{
let target_word = siv.find_id::<TextView>("target_field").unwrap();
if target_word.get_content() == input {
target_word.set_content(self.update_and_get_bar());
reset_input = true;
}
}
if reset_input {
siv.find_id::<EditView>("input_field").unwrap().set_content("");
}
}
fn rand_word(&self) -> &'static str {
let mut rng = rand::thread_rng();
rng.choose(&self.words).unwrap()
}
fn update_and_get_bar(&mut self) -> String {
if self.target_list.len() > 0 {
self.target_list.pop();
}
while self.target_list.len() < 5 {
let new_word = self.rand_word();
self.target_list.push(new_word);
}
let mut bar_text: String = "".to_string();
for word in &self.target_list {
if bar_text == "" {
bar_text = word.to_string();
} else {
bar_text.push_str(" ");
bar_text.push_str(word);
}
}
bar_text
}
}
とエラー
error: cannot borrow captured outer variable in an `Fn` closure as mutable
--> src/main.rs:20:45
|
20 | .on_edit(move |s, input, _| words.typed_some(s, input))
| ^^^^^
|
help: consider changing this closure to take self by mutable reference
--> src/main.rs:20:26
|
20 | .on_edit(move |s, input, _| words.typed_some(s, input))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Repo linkあなたはむしろそれのクローンを作成したい場合は、すべてのは、プッシュ。具体的に633ed60をコミットします。
); } (モバイルで)私はそれをテストすることはできません。 –
@WesleyWiserありがとうございますが、私は同じエラーが発生しました。ちょっと言い換えて言います。https://bpaste.net/show/dde9cfc1c7fe – Powersource
@WesleyWiser 'EditView :: on_edit'は' Fn'バウンドのクロージャを必要とします。環境(私の答えに示されているように 'RefCell'のようなエスケープハッチを除く)。 – user4815162342