rust


Rust closure errors -> …which is owned by the current function | capture of moved value:


This code:
//let seen_cell = std::cell::RefCell::new(window_0);
window_0.connect_delete_event(|_, _| {
//window_0.destroy();
window.hide();
Inhibit(true)
});
button_0.connect_clicked(|_|{
window.show_all();
}
);
Produces the errors:
error[E0373]: closure may outlive the current function, but it borrows `window`, which is owned by the current function
--> src/main.rs:192:36
|
192 | window_0.connect_delete_event( |_, _| {
| ^^^^^^ may outlive borrowed value `window`
...
195 | window.hide();
| ------ `window` is borrowed here
|
help: to force the closure to take ownership of `window` (and any other referenced variables), use the `move` keyword, as shown:
| window_0.connect_delete_event( move |_, _| {
error[E0373]: closure may outlive the current function, but it borrows `window`, which is owned by the current function
--> src/main.rs:199:30
|
199 | button_0.connect_clicked(|_|{
| ^^^ may outlive borrowed value `window`
200 | window.show_all();
| ------ `window` is borrowed here
|
help: to force the closure to take ownership of `window` (and any other referenced variables), use the `move` keyword, as shown:
| button_0.connect_clicked(move |_|{
If I try this:
//let seen_cell = std::cell::RefCell::new(window_0);
window_0.connect_delete_event(move |_, _| {
//window_0.destroy();
window.hide();
Inhibit(true)
});
button_0.connect_clicked(|_|{
window.show_all();
}
);
I get the errors:
error[E0373]: closure may outlive the current function, but it borrows `window`, which is owned by the current function
--> src/main.rs:199:30
|
199 | button_0.connect_clicked(|_|{
| ^^^ may outlive borrowed value `window`
200 | window.show_all();
| ------ `window` is borrowed here
|
help: to force the closure to take ownership of `window` (and any other referenced variables), use the `move` keyword, as shown:
| button_0.connect_clicked(move |_|{
error[E0382]: capture of moved value: `window`
--> src/main.rs:199:30
|
192 | window_0.connect_delete_event(move |_, _| {
| ----------- value moved (into closure) here
...
199 | button_0.connect_clicked(|_|{
| ^^^ value captured here after move
|
= note: move occurs because `window` has type `gtk::Window`, which does not implement the `Copy` trait
If I try this:
//let seen_cell = std::cell::RefCell::new(window_0);
window_0.connect_delete_event(move |_, _| {
//window_0.destroy();
window.hide();
Inhibit(true)
});
button_0.connect_clicked(move|_|{
window.show_all();
}
);
I get the errors:
error[E0382]: capture of moved value: `window`
--> src/main.rs:200:9
|
192 | window_0.connect_delete_event(move |_, _| {
| ----------- value moved (into closure) here
...
200 | window.show_all();
| ^^^^^^ value captured here after move
|
= note: move occurs because `window` has type `gtk::Window`, which does not implement the `Copy` trait
I have read similar questions but I have not been able to solve this case. How can I solve this in the best way, perhaps by using Arc or similar?
I have solved the above using a macro that I have drawn from some sample projects of gtk-rs
macro_rules! clone {
(#param _) => ( _ );
(#param $x:ident) => ( $x );
($($n:ident),+ => move || $body:expr) => (
{
$( let $n = $n.clone(); )+
move || $body
}
);
($($n:ident),+ => move |$($p:tt),+| $body:expr) => (
{
$( let $n = $n.clone(); )+
move |$(clone!(#param $p),)+| $body
}
);
}
Taking the case described I have used it this way:
window_0.connect_delete_event(clone!(window => move |_, _| {
//window_0.destroy();
window.hide();
Inhibit(true)
}));
button_0.connect_clicked(clone!(window => move |_|{
window.show_all();
}));
This is the relevant part window_0.connect_delete_event(clone!(window => move. In my case it also applies to button_0.connect_clicked because window Is used later in a similar place

Related Links

How can I succinctly combine many `Result`s of different types?
Why is the produced assembly not equivalent between returning by reference and copy when inlined?
include module from the same directory level [duplicate]
Is there a way to prevent emitted LLVM IR from removing unused functions?
Does Cargo support custom profiles?
Pass mutable reference to an element of a vector as parameter of function alongside with that vector
How to format a String object in Rust? [duplicate]
How do I make rustc-link-search relative to the project location?
Safe non-trivial data dependencies/custom references?
Skip struct field when serializing
Rust error E0495 using split_at_mut in a closure
What is the proper way to create a Span when inserting a new field with a Rust compiler plugin?
Using Any with traits in Rust [duplicate]
How do I create an iterator that invalidates the previous result each time `next` is called? [duplicate]
How can I better store a string to avoid many clones?
What's the difference between immutable and mutable references to an i32 when adding?

Categories

HOME
entity-framework
mod-rewrite
xcode8.1
compilation
adobe
iverilog
dronekit-python
portable-class-library
dji-sdk
azure-mobile-services
position
angular-meteor
pyresttest
static-analysis
log4j2
chromecast
createjs
stimulsoft
mahout-recommender
texas-instruments
async-await
exe
boomi
gmp
coroutine
azure-machine-learning
coreldraw
liferay-6.2
perfino
animated-gif
ratio
unrar
openbravo
android-service
boolean-logic
fatfs
envoy
steam-web-api
commonsware-cwac
rspec-rails
quickcheck
image-optimization
java-stream
cordys-opentext
uivisualeffectview
recovery
python-sounddevice
game-center
flipkart
haar-classifier
sendinput
axis
mongodb-aggregation
email-forwarding
mpmovieplayercontroller
rustdoc
java-2d
conan
maven-release-plugin
perforce-integrate
timestamping
jeasyui
mercurial-extension
management-studio-express
odesk
srv-record
apple
phpredis
scala-macros
incron
htmltidy
nhibernate-criteria
comctl32
window-resize
csslint
hyperloglog
radius-protocol
lync-2010
django-settings
illuminate-container
progress-db
stty
ldif
sunstudio
chefspec
examine
significant-digits
acpi
haiku
vows
data-dump
nosetests
kext
jquery-1.4
servlet-container
printqueue
libxslt
outlook-2007-addin
gtktextview
database-cloning
index.dat
levels
error-detection

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App