rust

How to set a variable inside a gtk-rs closure?

本秂侑毒 提交于 2020-01-30 11:16:10
问题 I'm building a markdown app, and I want to keep two copies of the text, one a source text, and the other the TextBuffer with all the correct tags and such. I need to set the contents of this source field inside a closure: buffer.connect_begin_user_action(clone!(source => move |a| { let text = a.get_text(&a.get_start_iter(), &a.get_end_iter(), false).unwrap(); source = text; // error: cannot assign to captured outer variable in an `Fn` closure An alternative might be to set some attribute on

The lifetime of self parameter in Rust when using threads [duplicate]

≯℡__Kan透↙ 提交于 2020-01-30 10:06:57
问题 This question already has an answer here : How can I pass a reference to a stack variable to a thread? (1 answer) Closed 5 months ago . I'm in the process of learning Rust and I have this code: use std::sync::{Arc, Mutex}; use std::thread::spawn; pub struct MyText { my_text: Mutex<Vec<String>>, } pub trait MyTextOptions { fn add(&self, t: String); } impl MyTextOptions for MyText { fn add(&self, text: String) { let int_text = Arc::new(self); let put_into_my_text = spawn(move || { let mut text

The lifetime of self parameter in Rust when using threads [duplicate]

天涯浪子 提交于 2020-01-30 10:03:05
问题 This question already has an answer here : How can I pass a reference to a stack variable to a thread? (1 answer) Closed 5 months ago . I'm in the process of learning Rust and I have this code: use std::sync::{Arc, Mutex}; use std::thread::spawn; pub struct MyText { my_text: Mutex<Vec<String>>, } pub trait MyTextOptions { fn add(&self, t: String); } impl MyTextOptions for MyText { fn add(&self, text: String) { let int_text = Arc::new(self); let put_into_my_text = spawn(move || { let mut text

Alternative way to handle GTK+ events in Rust

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-30 08:39:07
问题 Currently, I manage GTK+ events with Rc and RefCell as shown in the following example: extern crate gtk; use std::cell::RefCell; use std::rc::Rc; use gtk::{Button, ButtonExt, ContainerExt, Inhibit, Label, WidgetExt, Window, WindowType}; use gtk::Orientation::Vertical; struct Model { count: i32, } fn main() { gtk::init().unwrap(); let window = Window::new(WindowType::Toplevel); let model = Rc::new(RefCell::new(Model { count: 0 })); let vbox = gtk::Box::new(Vertical, 0); window.add(&vbox); let

Cannot borrow variable as mutable more than once at a time after calling a &'a mut self method

荒凉一梦 提交于 2020-01-30 08:09:07
问题 I have a problem with lifetimes/borrowing with my Graph object. fn main() { let mut g = Graph { nodePointer: &mut 0, edgePointer: &mut 0, nodes: &mut Vec::new(), edges: &mut Vec::new(), }; let node1 = g.add_node((1, 1)); let node2 = g.get_node(0); } pub struct Graph<'a> { pub nodePointer: &'a mut usize, pub edgePointer: &'a mut usize, pub nodes: &'a mut Vec<Node>, pub edges: &'a mut Vec<Edge>, } impl<'a> Graph<'a> { pub fn add_node(&'a mut self, data: (u64, u64)) -> usize { let id: usize =

Failed to infer type when using Result::map and Box

十年热恋 提交于 2020-01-30 08:08:47
问题 Why won't this compile? trait T {} fn f<U: 'static + T, V, E>(f2: V) -> impl Fn() -> Result<Box<dyn T>, E> where V: Fn() -> Result<U, E>, { move || -> Result<Box<dyn T>, E> { f2().map(Box::new) } } The error message is: error[E0308]: mismatched types --> src/lib.rs:7:40 | 7 | move || -> Result<Box<dyn T>, E> { f2().map(Box::new) } | ^^^^^^^^^^^^^^^^^^ expected trait T, found type parameter | = note: expected type `std::result::Result<std::boxed::Box<(dyn T + 'static)>, _>` found type `std:

Cannot borrow variable as mutable more than once at a time after calling a &'a mut self method

老子叫甜甜 提交于 2020-01-30 08:08:25
问题 I have a problem with lifetimes/borrowing with my Graph object. fn main() { let mut g = Graph { nodePointer: &mut 0, edgePointer: &mut 0, nodes: &mut Vec::new(), edges: &mut Vec::new(), }; let node1 = g.add_node((1, 1)); let node2 = g.get_node(0); } pub struct Graph<'a> { pub nodePointer: &'a mut usize, pub edgePointer: &'a mut usize, pub nodes: &'a mut Vec<Node>, pub edges: &'a mut Vec<Edge>, } impl<'a> Graph<'a> { pub fn add_node(&'a mut self, data: (u64, u64)) -> usize { let id: usize =

Failed to infer type when using Result::map and Box

纵然是瞬间 提交于 2020-01-30 08:08:12
问题 Why won't this compile? trait T {} fn f<U: 'static + T, V, E>(f2: V) -> impl Fn() -> Result<Box<dyn T>, E> where V: Fn() -> Result<U, E>, { move || -> Result<Box<dyn T>, E> { f2().map(Box::new) } } The error message is: error[E0308]: mismatched types --> src/lib.rs:7:40 | 7 | move || -> Result<Box<dyn T>, E> { f2().map(Box::new) } | ^^^^^^^^^^^^^^^^^^ expected trait T, found type parameter | = note: expected type `std::result::Result<std::boxed::Box<(dyn T + 'static)>, _>` found type `std: