Add a code example as comment in init_capture_kind_for_place

This commit is contained in:
Santiago Pastorino 2025-02-20 13:06:38 -03:00
parent 18d689c085
commit edcbc9b535
No known key found for this signature in database
GPG Key ID: 8131A24E0C79EFAF

View File

@ -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) =>
{