Skip to content

Commit 2fcdced

Browse files
committed
---
yaml --- r: 124950 b: refs/heads/auto c: 4357da3 h: refs/heads/master v: v3
1 parent fc31a43 commit 2fcdced

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0
1313
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
1414
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1515
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
16-
refs/heads/auto: c004bebc9ef12b5d6c24d0d482344ad485f2050b
16+
refs/heads/auto: 4357da356071e34278c5ef29aa1480360d0659b5
1717
refs/heads/servo: af82457af293e2a842ba6b7759b70288da276167
1818
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c
1919
refs/tags/0.1: b19db808c2793fe2976759b85a355c3ad8c8b336

branches/auto/src/libcollections/vec.rs

Lines changed: 72 additions & 1 deletion
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)