Ymi_Yugy
Ymi_Yugy

Reputation: 633

Returning slices in rust

#![feature(array_methods)]
#![feature(fixed_size_array)]

fn main() {
    foo();
    bar();
}

fn foo<'a>() -> &'a [i32] {
    return &[1,2,3];
}

fn bar<'a>() -> &'a [i32] {
    return goob().as_slice();
}

fn goob() -> [i32; 3] {
    return [1,2,3];
}

Why does foo run fine, but bar causes a compile error. Both return a slice from an array that gets temporarily created.

Upvotes: 1

Views: 34

Answers (0)

Related Questions