Skip to content

Commit 4357da3

Browse files
committed
doc: Fill vec documentation with examples.
Add more useful functions to main example.
1 parent c004beb commit 4357da3

File tree

1 file changed

+72
-1
lines changed

1 file changed

+72
-1
lines changed

src/libcollections/vec.rs

+72-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ pub static PTR_MARKER: u8 = 0;
4646
///
4747
/// assert_eq!(vec.pop(), Some(2));
4848
/// 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]);
4959
/// ```
5060
///
5161
/// The `vec!` macro is provided to make initialization more convenient:
@@ -56,6 +66,25 @@ pub static PTR_MARKER: u8 = 0;
5666
/// assert_eq!(vec, vec![1, 2, 3, 4]);
5767
/// ```
5868
///
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+
///
5988
/// # Capacity and reallocation
6089
///
6190
/// The capacity of a vector is the amount of space allocated for any future
@@ -766,6 +795,15 @@ impl<T> Vec<T> {
766795
/// This will explicitly set the size of the vector, without actually
767796
/// modifying its buffers, so it is up to the caller to ensure that the
768797
/// 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+
/// ```
769807
#[inline]
770808
pub unsafe fn set_len(&mut self, len: uint) {
771809
self.len = len;
@@ -1237,7 +1275,7 @@ impl<T> Vec<T> {
12371275
/// # Example
12381276
///
12391277
/// ```rust
1240-
/// let vec = vec![1i, 2, 3];
1278+
/// let vec = vec![1i, 2, 3, 4];
12411279
/// assert!(vec.slice_to(2) == [1, 2]);
12421280
/// ```
12431281
#[inline]
@@ -1250,6 +1288,13 @@ impl<T> Vec<T> {
12501288
/// # Failure
12511289
///
12521290
/// 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+
/// ```
12531298
#[inline]
12541299
pub fn init<'a>(&'a self) -> &'a [T] {
12551300
self.slice(0, self.len() - 1)
@@ -1263,6 +1308,19 @@ impl<T> Vec<T> {
12631308
///
12641309
/// Modifying the vector may cause its buffer to be reallocated, which
12651310
/// 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+
/// ```
12661324
#[inline]
12671325
pub fn as_ptr(&self) -> *const T {
12681326
self.ptr as *const T
@@ -1275,6 +1333,19 @@ impl<T> Vec<T> {
12751333
///
12761334
/// Modifying the vector may cause its buffer to be reallocated, which
12771335
/// 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+
/// ```
12781349
#[inline]
12791350
pub fn as_mut_ptr(&mut self) -> *mut T {
12801351
self.ptr

0 commit comments

Comments
 (0)