File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -352,6 +352,31 @@ impl<T: Clone> Vec<T> {
352
352
}
353
353
}
354
354
355
+ /// Resizes the `Vec` in-place so that `len()` equals to `new_len`.
356
+ ///
357
+ /// Calls either `grow()` or `truncate()` depending on whether `new_len` is
358
+ /// larger than the current value of `len()` or not.
359
+ ///
360
+ /// # Example
361
+ ///
362
+ /// ```
363
+ /// let mut vec = vec!["hello", "world"];
364
+ /// vec.resize(1, "world");
365
+ /// assert_eq!(vec!["hello"], vec);
366
+ /// vec.resize(3, "world");
367
+ /// assert_eq!(vec!["hello", "world", "world"], vec);
368
+ /// ```
369
+ #[ inline]
370
+ #[ experimental]
371
+ pub fn resize ( & mut self , new_len : uint , value : T ) {
372
+ let l = self . len ( ) ;
373
+ if l < new_len {
374
+ self . grow ( new_len - l, value)
375
+ } else {
376
+ self . truncate ( new_len)
377
+ }
378
+ }
379
+
355
380
/// Partitions a vector based on a predicate.
356
381
///
357
382
/// Clones the elements of the vector, partitioning them into two `Vec`s
You can’t perform that action at this time.
0 commit comments