| Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 18h ago | 1 | // Rust's `&[T]` is a borrow, not a copy. Nim's view types model that, |
| 2 | // including returning one: writing through the view reaches the original. |
| 3 | fn head(xs: &[i32], n: usize) -> &[i32] { |
| 4 | &xs[..n] |
| 5 | } |
| 6 | |
| 7 | fn middle(xs: &[i32]) -> &[i32] { |
| 8 | &xs[1..3] |
| 9 | } |
| 10 | |
| 11 | fn sum(xs: &[i32]) -> i32 { |
| 12 | let mut t: i32 = 0; |
| 13 | for x in xs.iter() { |
| 14 | t += x; |
| 15 | } |
| 16 | t |
| 17 | } |
| 18 | |
| 19 | fn main() { |
| 20 | let v: Vec<i32> = vec![1, 2, 3, 4, 5]; |
| 21 | println!("{}", sum(head(&v, 3))); |
| 22 | println!("{}", sum(middle(&v))); |
| 23 | println!("{}", head(&v, 2).len()); |
| 24 | println!("{}", head(&v, 5)[4]); |
| 25 | } |