Add a code example as comment in init_capture_kind_for_place
This commit is contained in:
parent
18d689c085
commit
edcbc9b535
1 changed files with 19 additions and 0 deletions
|
|
@ -1690,6 +1690,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
//
|
||||
// If the data will be moved out of this place, then the place will be truncated
|
||||
// at the first Deref in `adjust_for_move_closure` and then moved into the closure.
|
||||
//
|
||||
// For example:
|
||||
//
|
||||
// struct Buffer<'a> {
|
||||
// x: &'a String,
|
||||
// y: Vec<u8>,
|
||||
// }
|
||||
//
|
||||
// fn get<'a>(b: Buffer<'a>) -> impl Sized + 'a {
|
||||
// let c = move || b.x;
|
||||
// drop(b);
|
||||
// c
|
||||
// }
|
||||
//
|
||||
// Even though the closure is declared as move, when we are capturing borrowed data (in
|
||||
// this case, *b.x) we prefer to capture by reference.
|
||||
// Otherwise you'd get an error in 2021 immediately because you'd be trying to take
|
||||
// ownership of the (borrowed) String or else you'd take ownership of b, as in 2018 and
|
||||
// before, which is also an error.
|
||||
hir::CaptureBy::Value { .. } | hir::CaptureBy::Use { .. }
|
||||
if !place.deref_tys().any(Ty::is_ref) =>
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue