Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. #![feature(const_vec_new)]
  2. static mut vStrSlc: Vec<&str> = Vec::new();
  3. static mut vI32: Vec<i32> = Vec::new();
  4. trait Trait1 {
  5. fn f1(item: Self);
  6. fn f2()->Self;
  7. }
  8. impl Trait1 for &'static [&'static str] {
  9. fn f1(items: &'static [&'static str]) {
  10. for item in items {
  11. unsafe{vStrSlc.push(item);}
  12. }
  13. }
  14. fn f2()->&'static [&'static str]{
  15. let aLen = unsafe{vStrSlc.len()};
  16. static mut rslt:Vec<&str> = Vec::new();
  17. unsafe{
  18. rslt = vec![""; aLen];
  19. for i in 0..aLen {
  20. rslt[i] = vStrSlc.pop().unwrap();
  21. }
  22. rslt.as_slice()
  23. }
  24. }
  25. }
  26. impl Trait1 for &'static [i32] {
  27. fn f1(items: &'static [i32]) {
  28. for item in items {
  29. unsafe{vI32.push(*item);}
  30. }
  31. }
  32. fn f2()->&'static [i32]{
  33. let aLen = unsafe{vI32.len()};
  34. static mut rslt:Vec<i32> = Vec::new();
  35. unsafe{
  36. rslt = vec![0; aLen];
  37. for i in 0..aLen {
  38. rslt[i] = vI32.pop().unwrap();
  39. }
  40. rslt.as_slice()
  41. }
  42. }
  43. }
  44. fn main() {
  45. let arg:&'static [&'static str] = &["abc", "def", "ghi"];
  46. Trait1::f1(arg);
  47. println!("{:?}", <&'static [&'static str] as Trait1>::f2()); //["ghi", "def", "abc"]
  48. let arg:&'static [i32] = &[123, 456, 789];
  49. Trait1::f1(arg);
  50. println!("{:?}", <&'static [i32] as Trait1>::f2()); //[789, 456, 123]
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement