nandi/rustnimpublic Fork 0
af6e50f646055dc5b291c9e602bc9ee01874f9d6
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.

Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 · on af6e50f646055dc5b291c9e602bc9ee01874f9d6 · nandithebull · 19h ago
025-borrowed-slice-return.rs · 25 lines · 570 BRust Blame HistoryRaw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Rust's `&[T]` is a borrow, not a copy. Nim's view types model that,
// including returning one: writing through the view reaches the original.
fn head(xs: &[i32], n: usize) -> &[i32] {
    &xs[..n]
}

fn middle(xs: &[i32]) -> &[i32] {
    &xs[1..3]
}

fn sum(xs: &[i32]) -> i32 {
    let mut t: i32 = 0;
    for x in xs.iter() {
        t += x;
    }
    t
}

fn main() {
    let v: Vec<i32> = vec![1, 2, 3, 4, 5];
    println!("{}", sum(head(&v, 3)));
    println!("{}", sum(middle(&v)));
    println!("{}", head(&v, 2).len());
    println!("{}", head(&v, 5)[4]);
}