@@ -1199,6 +1199,31 @@ impl<T> VecDeque<T> {
11991199 }
12001200 }
12011201
1202+ /// Removes the last element from the `VecDeque` and returns it, or `None` if
1203+ /// it is empty.
1204+ ///
1205+ /// # Examples
1206+ ///
1207+ /// ```
1208+ /// use std::collections::VecDeque;
1209+ ///
1210+ /// let mut buf = VecDeque::new();
1211+ /// assert_eq!(buf.pop_back(), None);
1212+ /// buf.push_back(1);
1213+ /// buf.push_back(3);
1214+ /// assert_eq!(buf.pop_back(), Some(3));
1215+ /// ```
1216+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1217+ pub fn pop_back ( & mut self ) -> Option < T > {
1218+ if self . is_empty ( ) {
1219+ None
1220+ } else {
1221+ self . head = self . wrap_sub ( self . head , 1 ) ;
1222+ let head = self . head ;
1223+ unsafe { Some ( self . buffer_read ( head) ) }
1224+ }
1225+ }
1226+
12021227 /// Prepends an element to the `VecDeque`.
12031228 ///
12041229 /// # Examples
@@ -1243,38 +1268,13 @@ impl<T> VecDeque<T> {
12431268 unsafe { self . buffer_write ( head, value) }
12441269 }
12451270
1246- /// Removes the last element from the `VecDeque` and returns it, or `None` if
1247- /// it is empty.
1248- ///
1249- /// # Examples
1250- ///
1251- /// ```
1252- /// use std::collections::VecDeque;
1253- ///
1254- /// let mut buf = VecDeque::new();
1255- /// assert_eq!(buf.pop_back(), None);
1256- /// buf.push_back(1);
1257- /// buf.push_back(3);
1258- /// assert_eq!(buf.pop_back(), Some(3));
1259- /// ```
1260- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1261- pub fn pop_back ( & mut self ) -> Option < T > {
1262- if self . is_empty ( ) {
1263- None
1264- } else {
1265- self . head = self . wrap_sub ( self . head , 1 ) ;
1266- let head = self . head ;
1267- unsafe { Some ( self . buffer_read ( head) ) }
1268- }
1269- }
1270-
12711271 #[ inline]
12721272 fn is_contiguous ( & self ) -> bool {
12731273 self . tail <= self . head
12741274 }
12751275
1276- /// Removes an element from anywhere in the `VecDeque` and returns it, replacing it with the
1277- /// last element.
1276+ /// Removes an element from anywhere in the `VecDeque` and returns it,
1277+ /// replacing it with the first element.
12781278 ///
12791279 /// This does not preserve ordering, but is O(1).
12801280 ///
@@ -1288,28 +1288,28 @@ impl<T> VecDeque<T> {
12881288 /// use std::collections::VecDeque;
12891289 ///
12901290 /// let mut buf = VecDeque::new();
1291- /// assert_eq!(buf.swap_remove_back (0), None);
1291+ /// assert_eq!(buf.swap_remove_front (0), None);
12921292 /// buf.push_back(1);
12931293 /// buf.push_back(2);
12941294 /// buf.push_back(3);
12951295 /// assert_eq!(buf, [1, 2, 3]);
12961296 ///
1297- /// assert_eq!(buf.swap_remove_back(0 ), Some(1 ));
1298- /// assert_eq!(buf, [3, 2 ]);
1297+ /// assert_eq!(buf.swap_remove_front(2 ), Some(3 ));
1298+ /// assert_eq!(buf, [2, 1 ]);
12991299 /// ```
13001300 #[ stable( feature = "deque_extras_15" , since = "1.5.0" ) ]
1301- pub fn swap_remove_back ( & mut self , index : usize ) -> Option < T > {
1301+ pub fn swap_remove_front ( & mut self , index : usize ) -> Option < T > {
13021302 let length = self . len ( ) ;
1303- if length > 0 && index < length - 1 {
1304- self . swap ( index, length - 1 ) ;
1303+ if length > 0 && index < length && index != 0 {
1304+ self . swap ( index, 0 ) ;
13051305 } else if index >= length {
13061306 return None ;
13071307 }
1308- self . pop_back ( )
1308+ self . pop_front ( )
13091309 }
13101310
1311- /// Removes an element from anywhere in the `VecDeque` and returns it,
1312- /// replacing it with the first element.
1311+ /// Removes an element from anywhere in the `VecDeque` and returns it, replacing it with the
1312+ /// last element.
13131313 ///
13141314 /// This does not preserve ordering, but is O(1).
13151315 ///
@@ -1323,24 +1323,24 @@ impl<T> VecDeque<T> {
13231323 /// use std::collections::VecDeque;
13241324 ///
13251325 /// let mut buf = VecDeque::new();
1326- /// assert_eq!(buf.swap_remove_front (0), None);
1326+ /// assert_eq!(buf.swap_remove_back (0), None);
13271327 /// buf.push_back(1);
13281328 /// buf.push_back(2);
13291329 /// buf.push_back(3);
13301330 /// assert_eq!(buf, [1, 2, 3]);
13311331 ///
1332- /// assert_eq!(buf.swap_remove_front(2 ), Some(3 ));
1333- /// assert_eq!(buf, [2, 1 ]);
1332+ /// assert_eq!(buf.swap_remove_back(0 ), Some(1 ));
1333+ /// assert_eq!(buf, [3, 2 ]);
13341334 /// ```
13351335 #[ stable( feature = "deque_extras_15" , since = "1.5.0" ) ]
1336- pub fn swap_remove_front ( & mut self , index : usize ) -> Option < T > {
1336+ pub fn swap_remove_back ( & mut self , index : usize ) -> Option < T > {
13371337 let length = self . len ( ) ;
1338- if length > 0 && index < length && index != 0 {
1339- self . swap ( index, 0 ) ;
1338+ if length > 0 && index < length - 1 {
1339+ self . swap ( index, length - 1 ) ;
13401340 } else if index >= length {
13411341 return None ;
13421342 }
1343- self . pop_front ( )
1343+ self . pop_back ( )
13441344 }
13451345
13461346 /// Inserts an element at `index` within the `VecDeque`, shifting all elements with indices
0 commit comments