@@ -346,6 +346,29 @@ extern "rust-intrinsic" {
346
346
/// assert_eq!(b"Rust", [82, 117, 116, 116]);
347
347
///
348
348
///
349
+ /// // Turning a Vec<&T> into a Vec<Option<&T>>
350
+ /// let store = [0, 1, 2, 3];
351
+ /// let v_orig = store.iter().collect::<Vec<&i32>>();
352
+ /// // Using transmute; Undefined Behavior
353
+ /// let v_transmuted = mem::transmute::<Vec<&i32>, Vec<Option<&i32>>>(
354
+ /// v_orig);
355
+ /// // The suggested, safe way
356
+ /// let v_collected = v_orig.into_iter()
357
+ /// .map(|r| Some(r))
358
+ /// .collect::<Vec<Option<&i32>>>();
359
+ /// // The no-copy, unsafe way, still using transmute, but not UB
360
+ /// let v_no_copy = Vec::from_raw_parts(v_orig.as_mut_ptr(),
361
+ /// v_orig.len(),
362
+ /// v_orig.capacity());
363
+ /// mem::forget(v_orig);
364
+ /// // This is equivalent to the original, but safer, and reuses the same
365
+ /// // Vec internals. Therefore the new inner type must have the exact same
366
+ /// // size, and the same or lesser alignment, as the old type.
367
+ /// // The same caveats exist for this method as transmute, for the original
368
+ /// // inner type (`&i32`) to the converted inner type (`Option<&i32>`), so
369
+ /// // read the nomicon page linked above.
370
+ ///
371
+ ///
349
372
/// // Copying an `&mut T` to reslice:
350
373
/// fn split_at_mut_transmute<T>(slice: &mut [T], index: usize)
351
374
/// -> (&mut [T], &mut [T]) {
0 commit comments