SHARE
TWEET

Untitled

a guest Apr 19th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. macro_rules! my_vec {
  2.     ($y:expr, $($x:expr),*) => {
  3.         {
  4.             let mut temp_vec = Vec::with_capacity($y);
  5.             $(
  6.                 temp_vec.push($x);
  7.             )*
  8.             let len = temp_vec.len();
  9.             if len > $y {
  10.                 temp_vec.truncate($y);
  11.             }
  12.             temp_vec
  13.         }
  14.     };
  15. }
  16.  
  17. fn main() {
  18.     assert_eq!(my_vec![1, 1, 2, 3], vec![1]);
  19.     assert_eq!(my_vec![2, 1, 2, 3], vec![1, 2]);
  20.     assert_eq!(my_vec![3, 1, 2, 3], vec![1, 2, 3]);
  21.     assert_eq!(my_vec![4, 1, 2, 3], vec![1, 2, 3]);
  22. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top