@@ -46,6 +46,16 @@ pub static PTR_MARKER: u8 = 0;
46
46
///
47
47
/// assert_eq!(vec.pop(), Some(2));
48
48
/// assert_eq!(vec.len(), 1);
49
+ ///
50
+ /// *vec.get_mut(0) = 7i;
51
+ /// assert_eq!(vec[0], 7);
52
+ ///
53
+ /// vec.push_all([1, 2, 3]);
54
+ ///
55
+ /// for x in vec.iter() {
56
+ /// println!("{}", x);
57
+ /// }
58
+ /// assert_eq!(vec, vec![7i, 1, 2, 3]);
49
59
/// ```
50
60
///
51
61
/// The `vec!` macro is provided to make initialization more convenient:
@@ -56,6 +66,25 @@ pub static PTR_MARKER: u8 = 0;
56
66
/// assert_eq!(vec, vec![1, 2, 3, 4]);
57
67
/// ```
58
68
///
69
+ /// Use a `Vec` as an efficient stack:
70
+ ///
71
+ /// ```
72
+ /// let mut stack = Vec::new();
73
+ ///
74
+ /// stack.push(1i);
75
+ /// stack.push(2i);
76
+ /// stack.push(3i);
77
+ ///
78
+ /// loop {
79
+ /// let top = match stack.pop() {
80
+ /// None => break, // empty
81
+ /// Some(x) => x,
82
+ /// };
83
+ /// // Prints 3, 2, 1
84
+ /// println!("{}", top);
85
+ /// }
86
+ /// ```
87
+ ///
59
88
/// # Capacity and reallocation
60
89
///
61
90
/// The capacity of a vector is the amount of space allocated for any future
@@ -766,6 +795,15 @@ impl<T> Vec<T> {
766
795
/// This will explicitly set the size of the vector, without actually
767
796
/// modifying its buffers, so it is up to the caller to ensure that the
768
797
/// vector is actually the specified size.
798
+ ///
799
+ /// # Example
800
+ ///
801
+ /// ```
802
+ /// let mut v = vec![1u, 2, 3, 4];
803
+ /// unsafe {
804
+ /// v.set_len(1);
805
+ /// }
806
+ /// ```
769
807
#[ inline]
770
808
pub unsafe fn set_len ( & mut self , len : uint ) {
771
809
self . len = len;
@@ -1237,7 +1275,7 @@ impl<T> Vec<T> {
1237
1275
/// # Example
1238
1276
///
1239
1277
/// ```rust
1240
- /// let vec = vec![1i, 2, 3];
1278
+ /// let vec = vec![1i, 2, 3, 4 ];
1241
1279
/// assert!(vec.slice_to(2) == [1, 2]);
1242
1280
/// ```
1243
1281
#[ inline]
@@ -1250,6 +1288,13 @@ impl<T> Vec<T> {
1250
1288
/// # Failure
1251
1289
///
1252
1290
/// Fails if the vector is empty
1291
+ ///
1292
+ /// # Example
1293
+ ///
1294
+ /// ```
1295
+ /// let vec = vec![1i, 2, 3];
1296
+ /// assert!(vec.init() == [1, 2]);
1297
+ /// ```
1253
1298
#[ inline]
1254
1299
pub fn init < ' a > ( & ' a self ) -> & ' a [ T ] {
1255
1300
self . slice ( 0 , self . len ( ) - 1 )
@@ -1263,6 +1308,19 @@ impl<T> Vec<T> {
1263
1308
///
1264
1309
/// Modifying the vector may cause its buffer to be reallocated, which
1265
1310
/// would also make any pointers to it invalid.
1311
+ ///
1312
+ /// # Example
1313
+ ///
1314
+ /// ```
1315
+ /// use std::vec::raw;
1316
+ ///
1317
+ /// let v = vec![1i, 2, 3];
1318
+ /// let p = v.as_ptr();
1319
+ /// unsafe {
1320
+ /// let b = raw::from_buf(p, 3u);
1321
+ /// assert_eq!(b, vec![1i, 2, 3]);
1322
+ /// }
1323
+ /// ```
1266
1324
#[ inline]
1267
1325
pub fn as_ptr ( & self ) -> * const T {
1268
1326
self . ptr as * const T
@@ -1275,6 +1333,19 @@ impl<T> Vec<T> {
1275
1333
///
1276
1334
/// Modifying the vector may cause its buffer to be reallocated, which
1277
1335
/// would also make any pointers to it invalid.
1336
+ ///
1337
+ /// # Example
1338
+ ///
1339
+ /// ```
1340
+ /// use std::ptr;
1341
+ ///
1342
+ /// let mut v = vec![1i, 2, 3];
1343
+ /// let p = v.as_mut_ptr();
1344
+ /// unsafe {
1345
+ /// ptr::write(p, 9i);
1346
+ /// }
1347
+ /// assert_eq!(v, vec![9i, 2, 3]);
1348
+ /// ```
1278
1349
#[ inline]
1279
1350
pub fn as_mut_ptr ( & mut self ) -> * mut T {
1280
1351
self . ptr
0 commit comments