File tree 1 file changed +34
-0
lines changed
1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -1367,6 +1367,40 @@ impl<T> Vec<T> {
1367
1367
self . truncate ( new_len) ;
1368
1368
}
1369
1369
}
1370
+
1371
+ /// Consumes and leaks the `Vec`, returning a mutable reference to the contents,
1372
+ /// `&'a mut [T]`. Note that the type `T` must outlive the chosen lifetime
1373
+ /// `'a`. If the type has only static references, or none at all, then this
1374
+ /// may be chosen to be `'static`.
1375
+ ///
1376
+ /// This function is similar to the `leak` function on `Box`.
1377
+ ///
1378
+ /// This function is mainly useful for data that lives for the remainder of
1379
+ /// the program's life. Dropping the returned reference will cause a memory
1380
+ /// leak.
1381
+ ///
1382
+ /// # Examples
1383
+ ///
1384
+ /// Simple usage:
1385
+ ///
1386
+ /// ```
1387
+ /// #![feature(vec_leak)]
1388
+ ///
1389
+ /// fn main() {
1390
+ /// let x = vec![1, 2, 3];
1391
+ /// let static_ref: &'static mut [usize] = Vec::leak(x);
1392
+ /// static_ref[0] += 1;
1393
+ /// assert_eq!(static_ref, &[2, 2, 3]);
1394
+ /// }
1395
+ /// ```
1396
+ #[ unstable( feature = "vec_leak" , issue = "62195" ) ]
1397
+ #[ inline]
1398
+ pub fn leak < ' a > ( vec : Vec < T > ) -> & ' a mut [ T ]
1399
+ where
1400
+ T : ' a // Technically not needed, but kept to be explicit.
1401
+ {
1402
+ Box :: leak ( vec. into_boxed_slice ( ) )
1403
+ }
1370
1404
}
1371
1405
1372
1406
impl < T : Clone > Vec < T > {
You can’t perform that action at this time.
0 commit comments