Skip to content

Commit 6ce503c

Browse files
Rollup merge of rust-lang#62250 - czipperz:improve-box-clone-doctests, r=GuillaumeGomez
Improve box clone doctests to ensure the documentation is valid
2 parents e9a218c + fc70c37 commit 6ce503c

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/liballoc/boxed.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -367,23 +367,35 @@ impl<T: Clone> Clone for Box<T> {
367367
/// ```
368368
/// let x = Box::new(5);
369369
/// let y = x.clone();
370+
///
371+
/// // The value is the same
372+
/// assert_eq!(x, y);
373+
///
374+
/// // But they are unique objects
375+
/// assert_ne!(&*x as *const i32, &*y as *const i32);
370376
/// ```
371377
#[rustfmt::skip]
372378
#[inline]
373379
fn clone(&self) -> Box<T> {
374380
box { (**self).clone() }
375381
}
382+
376383
/// Copies `source`'s contents into `self` without creating a new allocation.
377384
///
378385
/// # Examples
379386
///
380387
/// ```
381388
/// let x = Box::new(5);
382389
/// let mut y = Box::new(10);
390+
/// let yp: *const i32 = &*y;
383391
///
384392
/// y.clone_from(&x);
385393
///
386-
/// assert_eq!(*y, 5);
394+
/// // The value is the same
395+
/// assert_eq!(x, y);
396+
///
397+
/// // And no allocation occurred
398+
/// assert_eq!(yp, &*y);
387399
/// ```
388400
#[inline]
389401
fn clone_from(&mut self, source: &Box<T>) {

0 commit comments

Comments
 (0)