phpify/array/array_pop.rs
1// Copyright (c) 2020 DarkWeb Design
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
16// SOFTWARE.
17
18/// Pop the element off the end of vector
19///
20/// # Description
21///
22/// array_pop() pops and returns the value of the last element of vector, shortening the vector by
23/// one element.
24///
25/// # Examples
26///
27/// Example #1 array_pop() example
28///
29/// ```
30/// use phpify::array::array_pop;
31///
32/// let mut stack = vec!["orange", "banana", "apple", "raspberry"];
33/// let fruit = array_pop(&mut stack).unwrap();
34///
35/// assert_eq!(stack, vec!["orange", "banana", "apple"]);
36/// assert_eq!(fruit, "raspberry");
37/// ```
38pub fn array_pop<T>(array: &mut Vec<T>) -> Option<T> {
39 array.pop()
40}
41
42#[cfg(test)]
43mod tests {
44 use crate::array::array_pop;
45
46 #[test]
47 fn test() {
48 let mut vec = vec!["a", "b", "c"];
49 assert_eq!(array_pop(&mut vec), Some("c"));
50 assert_eq!(vec, vec!["a", "b"]);
51 assert_eq!(array_pop::<usize>(&mut Vec::new()), None);
52 }
53}