@@ -330,7 +330,7 @@ impl<T: ?Sized> *const T {
330
330
///
331
331
/// unsafe {
332
332
/// if let Some(val_back) = ptr.as_ref() {
333
- /// println!("We got back the value: { val_back}!" );
333
+ /// assert_eq!( val_back, &10 );
334
334
/// }
335
335
/// }
336
336
/// ```
@@ -346,7 +346,7 @@ impl<T: ?Sized> *const T {
346
346
///
347
347
/// unsafe {
348
348
/// let val_back = &*ptr;
349
- /// println!("We got back the value: { val_back}!" );
349
+ /// assert_eq!( val_back, &10 );
350
350
/// }
351
351
/// ```
352
352
#[ stable( feature = "ptr_as_ref" , since = "1.9.0" ) ]
@@ -393,7 +393,7 @@ impl<T: ?Sized> *const T {
393
393
/// let ptr: *const u8 = &10u8 as *const u8;
394
394
///
395
395
/// unsafe {
396
- /// println!("We got back the value: {}!", ptr.as_ref_unchecked());
396
+ /// assert_eq!( ptr.as_ref_unchecked(), &10 );
397
397
/// }
398
398
/// ```
399
399
// FIXME: mention it in the docs for `as_ref` and `as_uninit_ref` once stabilized.
@@ -439,7 +439,7 @@ impl<T: ?Sized> *const T {
439
439
///
440
440
/// unsafe {
441
441
/// if let Some(val_back) = ptr.as_uninit_ref() {
442
- /// println!("We got back the value: {}!", val_back.assume_init());
442
+ /// assert_eq!( val_back.assume_init(), 10 );
443
443
/// }
444
444
/// }
445
445
/// ```
@@ -501,8 +501,8 @@ impl<T: ?Sized> *const T {
501
501
/// let ptr: *const u8 = s.as_ptr();
502
502
///
503
503
/// unsafe {
504
- /// println!("{}", *ptr.offset(1) as char);
505
- /// println!("{}", *ptr.offset(2) as char);
504
+ /// assert_eq!( *ptr.offset(1) as char, '2' );
505
+ /// assert_eq!( *ptr.offset(2) as char, '3' );
506
506
/// }
507
507
/// ```
508
508
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -573,19 +573,21 @@ impl<T: ?Sized> *const T {
573
573
/// # Examples
574
574
///
575
575
/// ```
576
+ /// # use std::fmt::Write;
576
577
/// // Iterate using a raw pointer in increments of two elements
577
578
/// let data = [1u8, 2, 3, 4, 5];
578
579
/// let mut ptr: *const u8 = data.as_ptr();
579
580
/// let step = 2;
580
581
/// let end_rounded_up = ptr.wrapping_offset(6);
581
582
///
582
- /// // This loop prints "1, 3, 5, "
583
+ /// let mut out = String::new();
583
584
/// while ptr != end_rounded_up {
584
585
/// unsafe {
585
- /// print!( "{}, ", *ptr);
586
+ /// write!(&mut out, "{}, ", *ptr).unwrap( );
586
587
/// }
587
588
/// ptr = ptr.wrapping_offset(step);
588
589
/// }
590
+ /// assert_eq!(out.as_str(), "1, 3, 5, ");
589
591
/// ```
590
592
#[ stable( feature = "ptr_wrapping_offset" , since = "1.16.0" ) ]
591
593
#[ must_use = "returns a new pointer rather than modifying its argument" ]
@@ -988,8 +990,8 @@ impl<T: ?Sized> *const T {
988
990
/// let ptr: *const u8 = s.as_ptr();
989
991
///
990
992
/// unsafe {
991
- /// println!("{}", *ptr.add(1) as char );
992
- /// println!("{}", *ptr.add(2) as char );
993
+ /// assert_eq!( *ptr.add(1), b'2' );
994
+ /// assert_eq!( *ptr.add(2), b'3' );
993
995
/// }
994
996
/// ```
995
997
#[ stable( feature = "pointer_methods" , since = "1.26.0" ) ]
@@ -1073,8 +1075,8 @@ impl<T: ?Sized> *const T {
1073
1075
///
1074
1076
/// unsafe {
1075
1077
/// let end: *const u8 = s.as_ptr().add(3);
1076
- /// println!("{}", *end.sub(1) as char );
1077
- /// println!("{}", *end.sub(2) as char );
1078
+ /// assert_eq!( *end.sub(1), b'3' );
1079
+ /// assert_eq!( *end.sub(2), b'2' );
1078
1080
/// }
1079
1081
/// ```
1080
1082
#[ stable( feature = "pointer_methods" , since = "1.26.0" ) ]
@@ -1155,19 +1157,21 @@ impl<T: ?Sized> *const T {
1155
1157
/// # Examples
1156
1158
///
1157
1159
/// ```
1160
+ /// # use std::fmt::Write;
1158
1161
/// // Iterate using a raw pointer in increments of two elements
1159
1162
/// let data = [1u8, 2, 3, 4, 5];
1160
1163
/// let mut ptr: *const u8 = data.as_ptr();
1161
1164
/// let step = 2;
1162
1165
/// let end_rounded_up = ptr.wrapping_add(6);
1163
1166
///
1164
- /// // This loop prints "1, 3, 5, "
1167
+ /// let mut out = String::new();
1165
1168
/// while ptr != end_rounded_up {
1166
1169
/// unsafe {
1167
- /// print!( "{}, ", *ptr);
1170
+ /// write!(&mut out, "{}, ", *ptr).unwrap( );
1168
1171
/// }
1169
1172
/// ptr = ptr.wrapping_add(step);
1170
1173
/// }
1174
+ /// assert_eq!(out, "1, 3, 5, ");
1171
1175
/// ```
1172
1176
#[ stable( feature = "pointer_methods" , since = "1.26.0" ) ]
1173
1177
#[ must_use = "returns a new pointer rather than modifying its argument" ]
@@ -1234,19 +1238,21 @@ impl<T: ?Sized> *const T {
1234
1238
/// # Examples
1235
1239
///
1236
1240
/// ```
1241
+ /// # use std::fmt::Write;
1237
1242
/// // Iterate using a raw pointer in increments of two elements (backwards)
1238
1243
/// let data = [1u8, 2, 3, 4, 5];
1239
1244
/// let mut ptr: *const u8 = data.as_ptr();
1240
1245
/// let start_rounded_down = ptr.wrapping_sub(2);
1241
1246
/// ptr = ptr.wrapping_add(4);
1242
1247
/// let step = 2;
1243
- /// // This loop prints "5, 3, 1, "
1248
+ /// let mut out = String::new();
1244
1249
/// while ptr != start_rounded_down {
1245
1250
/// unsafe {
1246
- /// print!( "{}, ", *ptr);
1251
+ /// write!(&mut out, "{}, ", *ptr).unwrap( );
1247
1252
/// }
1248
1253
/// ptr = ptr.wrapping_sub(step);
1249
1254
/// }
1255
+ /// assert_eq!(out, "5, 3, 1, ");
1250
1256
/// ```
1251
1257
#[ stable( feature = "pointer_methods" , since = "1.26.0" ) ]
1252
1258
#[ must_use = "returns a new pointer rather than modifying its argument" ]
0 commit comments