Skip to content

Commit 0dbf429

Browse files
authored
Merge pull request #1702 from UnHumbleBen/patch-2
Fixes description of Closure lifetime in comments.
2 parents c7180dd + 89067c1 commit 0dbf429

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

examples/closures/src/lib.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ fn setup_clock(window: &Window, document: &Document) -> Result<(), JsValue> {
7272
));
7373
}
7474

75-
// The instances of `Closure` that we created will invalidate their
76-
// corresponding JS callback whenever they're dropped, so if we were to
77-
// normally return from `run` then both of our registered closures will
78-
// raise exceptions when invoked.
75+
// The instance of `Closure` that we created will invalidate its
76+
// corresponding JS callback whenever it is dropped, so if we were to
77+
// normally return from `setup_clock` then our registered closure will
78+
// raise an exception when invoked.
7979
//
80-
// Normally we'd store these handles to later get dropped at an appropriate
81-
// time but for now we want these to be global handlers so we use the
82-
// `forget` method to drop them without invalidating the closure. Note that
80+
// Normally we'd store the handle to later get dropped at an appropriate
81+
// time but for now we want it to be a global handler so we use the
82+
// `forget` method to drop it without invalidating the closure. Note that
8383
// this is leaking memory in Rust, so this should be done judiciously!
8484
a.forget();
8585

@@ -106,5 +106,7 @@ fn setup_clicker(document: &Document) {
106106
.dyn_ref::<HtmlElement>()
107107
.expect("#green-square be an `HtmlElement`")
108108
.set_onclick(Some(a.as_ref().unchecked_ref()));
109+
110+
// See comments in `setup_clock` above for why we use `a.forget()`.
109111
a.forget();
110112
}

0 commit comments

Comments
 (0)