@@ -312,30 +312,34 @@ extern "rust-intrinsic" {
312
312
/// are as follows:
313
313
///
314
314
/// Turning a pointer into a `usize`:
315
+ ///
315
316
/// ```
316
317
/// let ptr = &0;
317
318
/// let ptr_num_transmute = mem::transmute::<&i32, usize>(ptr);
318
- /// // Use `as` casts instead
319
+ /// // Use an `as` cast instead
319
320
/// let ptr_num_cast = ptr as *const i32 as usize;
320
321
/// ```
321
322
///
322
323
/// Turning a `*mut T` into an `&mut T`:
324
+ ///
323
325
/// ```
324
326
/// let ptr: *mut i32 = &mut 0;
325
327
/// let ref_transmuted = mem::transmute::<*mut i32, &mut i32>(ptr);
326
- /// // Use reborrows
328
+ /// // Use a reborrow instead
327
329
/// let ref_casted = &mut *ptr;
328
330
/// ```
329
331
///
330
332
/// Turning an `&mut T` into an `&mut U`:
333
+ ///
331
334
/// ```
332
335
/// let ptr = &mut 0;
333
336
/// let val_transmuted = mem::transmute::<&mut i32, &mut u32>(ptr);
334
- /// // Now let's put together `as` and reborrowing
337
+ /// // Now, put together `as` and reborrowing
335
338
/// let val_casts = &mut *(ptr as *mut i32 as *mut u32);
336
339
/// ```
337
340
///
338
341
/// Turning an `&str` into an `&[u8]`:
342
+ ///
339
343
/// ```
340
344
/// // this is not a good way to do this.
341
345
/// let slice = unsafe { mem::transmute::<&str, &[u8]>("Rust") };
@@ -349,20 +353,21 @@ extern "rust-intrinsic" {
349
353
/// ```
350
354
///
351
355
/// Turning a `Vec<&T>` into a `Vec<Option<&T>>`:
356
+ ///
352
357
/// ```
353
358
/// let store = [0, 1, 2, 3];
354
359
/// let v_orig = store.iter().collect::<Vec<&i32>>();
355
- /// // Using transmute: this is Undefined Behavior, and a bad idea
356
- /// // However, it is no-copy
360
+ /// // Using transmute: this is Undefined Behavior, and a bad idea.
361
+ /// // However, it is no-copy.
357
362
/// let v_transmuted = mem::transmute::<Vec<&i32>, Vec<Option<&i32>>>(
358
363
/// v_orig.clone());
359
- /// // This is the suggested, safe way
360
- /// // It does copy the entire Vector, though, into a new array
364
+ /// // This is the suggested, safe way.
365
+ /// // It does copy the entire Vector, though, into a new array.
361
366
/// let v_collected = v_orig.clone()
362
367
/// .into_iter()
363
368
/// .map(|r| Some(r))
364
369
/// .collect::<Vec<Option<&i32>>>();
365
- /// // The no-copy, unsafe way, still using transmute, but not UB
370
+ /// // The no-copy, unsafe way, still using transmute, but not UB.
366
371
/// // This is equivalent to the original, but safer, and reuses the
367
372
/// // same Vec internals. Therefore the new inner type must have the
368
373
/// // exact same size, and the same or lesser alignment, as the old
@@ -375,11 +380,12 @@ extern "rust-intrinsic" {
375
380
/// mem::forget(v_orig);
376
381
/// ```
377
382
///
378
- /// Implemententing `split_at_mut`:
383
+ /// Implementing `split_at_mut`:
384
+ ///
379
385
/// ```
380
386
/// use std::{slice, mem};
381
387
/// // There are multiple ways to do this; and there are multiple problems
382
- /// // with the following, transmute, way
388
+ /// // with the following, transmute, way.
383
389
/// fn split_at_mut_transmute<T>(slice: &mut [T], index: usize)
384
390
/// -> (&mut [T], &mut [T]) {
385
391
/// let len = slice.len();
@@ -388,20 +394,20 @@ extern "rust-intrinsic" {
388
394
/// let slice2 = mem::transmute::<&mut [T], &mut [T]>(slice);
389
395
/// // first: transmute is not typesafe; all it checks is that T and
390
396
/// // U are of the same size. Second, right here, you have two
391
- /// // mutable references pointing to the same memory
397
+ /// // mutable references pointing to the same memory.
392
398
/// (&mut slice[0..index], &mut slice2[index..len])
393
399
/// }
394
400
/// }
395
401
/// // This gets rid of the typesafety problems; `&mut *` will *only* give
396
- /// // you an &mut T from an &mut T or *mut T
402
+ /// // you an ` &mut T` from an ` &mut T` or ` *mut T`.
397
403
/// fn split_at_mut_casts<T>(slice: &mut [T], index: usize)
398
404
/// -> (&mut [T], &mut [T]) {
399
405
/// let len = slice.len();
400
406
/// assert!(index < len);
401
407
/// unsafe {
402
408
/// let slice2 = &mut *(slice as *mut [T]);
403
409
/// // however, you still have two mutable references pointing to
404
- /// // the same memory
410
+ /// // the same memory.
405
411
/// (&mut slice[0..index], &mut slice2[index..len])
406
412
/// }
407
413
/// }
@@ -415,9 +421,9 @@ extern "rust-intrinsic" {
415
421
/// assert!(mid <= len);
416
422
/// // This now has three mutable references pointing at the same
417
423
/// // memory. `slice`, the rvalue ret.0, and the rvalue ret.1.
418
- /// // However, `slice` is never used after `let ptr = ...`, and so
419
- /// // one can treat it as "dead", and therefore, you only have two
420
- /// // real mutable slices.
424
+ /// // `slice` is never used after `let ptr = ...`, and so one can
425
+ /// // treat it as "dead", and therefore, you only have two real
426
+ /// // mutable slices.
421
427
/// (slice::from_raw_parts_mut(ptr, mid),
422
428
/// slice::from_raw_parts_mut(ptr.offset(mid as isize), len - mid))
423
429
/// }
@@ -429,13 +435,15 @@ extern "rust-intrinsic" {
429
435
/// There are valid uses of transmute, though they are few and far between.
430
436
///
431
437
/// Getting the bitpattern of a floating point type:
438
+ ///
432
439
/// ```
433
440
/// let bitpattern = std::mem::transmute::<f32, u32>(1.0);
434
441
/// assert_eq!(bitpattern, 0x3F800000);
435
442
/// ```
436
443
///
437
444
/// Turning a pointer into a function pointer (this isn't guaranteed to
438
445
/// work in Rust, although, for example, Linux does make this guarantee):
446
+ ///
439
447
/// ```
440
448
/// fn foo() -> i32 {
441
449
/// 0
@@ -447,6 +455,7 @@ extern "rust-intrinsic" {
447
455
///
448
456
/// Extending a lifetime, or shortening an invariant an invariant lifetime;
449
457
/// this is advanced, very unsafe rust:
458
+ ///
450
459
/// ```
451
460
/// use std::mem;
452
461
///
0 commit comments