1
#![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.

Ymi_Yugy
  • 513
  • 1
  • 4
  • 7
  • whars the error? – leoOrion Oct 28 '20 at 02:26
  • TL;DR the duplicate: literals are "promoted" to statics in lvalue contexts, but `goob()` is not a literal. The only way for this to work is if you put the result of `goob()` in a `static` (`const` also works). – trent Oct 28 '20 at 04:04

0 Answers0