https://github.com/rust-lang/project-rfc-2229/issues/49
let x: Box<[u8; 1024]> = ...;
let c = move || x.len();
this used to copy 1 pointer, but if we capture *x
it would copy 1024 bytes.
struct MyStruct {
a: A,
b: B,
c: C,
}
fn foo(m: &MyStruct) {
let c = || (&m.a, &m.b);
// might as well just capture `m` and not the two fields individually
}
// example 1
struct MyStruct<'a> {
a: A,
b: B,
c: C<'a>,
}
(`(()))
// example 2
struct MyStruct<'a> {
a: &'static A,
b: B,
c: C<'a>,
}
fn foo<'a, 'b>(m: &'a MyStruct<'b>) -> impl FnMut() + 'static {
let c = || drop(&*m.a.field_of_a);
// Here we really do want to capture `*m.a` because that outlives `'static`
// If we capture `m`, then the closure no longer outlives `'static'
// it is constrained to `'a`
}
a safe rule would be:
x
of type &T
and you borrow a set of paths (*x).field{1...N}
you could instead just capture x
&*m.a
example becauseExample 1:
(*m).a
and borrow (*m).b
:
m
by value (guaranteed to be copy)Example 2:
(*(*m).a).field_of_a
(*m).a
by value (guaranteed to be copy)Niko thinks this is a safe optimization
or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Syncing