nandi/rustnimpublic Fork 0
main
Commits
Clone
git clone https://git.rickub.com/nandi/rustnim.git
git clone ssh://git@rickub.com/nandi/rustnim.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

025-borrowed-slice-return.rs · 25 lines · 570 BRust Blame HistoryRaw
Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 5h ago1// 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.
3fn head(xs: &[i32], n: usize) -> &[i32] {
4 &xs[..n]
5}
6
7fn middle(xs: &[i32]) -> &[i32] {
8 &xs[1..3]
9}
10
11fn sum(xs: &[i32]) -> i32 {
12 let mut t: i32 = 0;
13 for x in xs.iter() {
14 t += x;
15 }
16 t
17}
18
19fn 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}