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 to parse i64 from a string? [duplicate]
Why is variable scope dependent on the definition order?
Warning on “unstable” directive
How can I pass a socket as an argument to a function being called within a thread?
error: cannot move out of borrowed content on &mut self
Why does a reference not live long enough in case of “as_slice”?
Getting the error “error: the trait `core::marker::Sized` is not implemented” when trying to return the value from a vector
Traits with associated type in templates
Error handling best-practices
How to call count on an iterator and still use the iterator's items?
Caught between a lifetime and an FFI place
How to swap elements of array?
error: cannot assign to immutable indexed content `i[..]`
How to make a vector of received size?
Make an argument of type BTreeMap optional [closed]
Why can I assign a negative number to an unsigned integer variable?

Categories

HOME
xpath
angular-ui-grid
yahoo-weather-api
dafny
embedded-resource
google-webmaster-tools
fogbugz
drag-and-drop
share
supercollider
u-boot
angular2-template
mailing-list
wine
phpstorm-2017.1
createjs
sql-injection
squid
reselect
nest-api
exec-maven-plugin
boomi
bluemix-mobile-services
multi-upload
mod-wsgi
ccavenue
code-climate
jsonresult
windowsiot
rust-cargo
caesar-cipher
nest
directfb
transform
code-rally
macports
cheat-engine
assemblies
get-event-store
vb.net-to-c#
access-denied
dbcc
nomad
react-intl
rightnow-crm
ssi
ilrepack
openstack-glance
asp.net-web-api-routing
wikimapia
ternary-operator
java-2d
nsrunloop
livecycle
openseadragon
declarative
qpixmap
maximize
simian
vimperator
diawi
opencyc
place
supercomputers
ngcordova
uitest
com0com
console.log
text-align
quantify
code-complexity
property-injection
usb-flash-drive
principalcontext
pbkdf2
visual-c++-2005
infinite
.net-remoting
xs
session-0-isolation
user-forums
multiple-dispatch
xcode4.5
platform-independent
series-40
glx
expression-web
jquery-1.4
dip
icicles
aspbutton
jquery-effects
subtext
icon-language
stretchblt
jboss-mdb
rootkit

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App