Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=faa286146f7760bc7c1be85eaac73f9d
const OPTIONAL_SLICE_V1: Option<&'static [u8]> = Some(&{
let array = [1, 2, 3];
array
});
The current output is (both on stable, beta, and nightly):
error[[E0716]](https://doc.rust-lang.org/stable/error-index.html#E0716): temporary value dropped while borrowed
--> src/lib.rs:1:56
|
1 | const OPTIONAL_SLICE_V1: Option<&'static [u8]> = Some(&{
| __________________________________________________-_____^
| |__________________________________________________|
| ||
2 | || let array = [1, 2, 3];
3 | || array
4 | || });
| || ^-
| || ||
| ||_|temporary value is freed at the end of this statement
| |_|using this value as a constant requires that borrow lasts for `'static`
| creates a temporary which is freed while still in use
For more information about this error, try `rustc --explain E0716`.
It is, however, possible to get this code to compile by using the struct initialiser syntax for the tuple variant:
const OPTIONAL_SLICE_V2: Option<&'static [u8]> = Some {
0: &{
let array = [1, 2, 3];
array
},
};
Given that this initialiser isn't used very often, it took me a while to remember it as an alternative and to figure out that the use of Some as a function was causing the error.
Ideally, the error message should also suggest something like
help: try using a struct initializer instead: `Some { 0: ... }`
Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=faa286146f7760bc7c1be85eaac73f9d
The current output is (both on stable, beta, and nightly):
It is, however, possible to get this code to compile by using the struct initialiser syntax for the tuple variant:
Given that this initialiser isn't used very often, it took me a while to remember it as an alternative and to figure out that the use of
Someas a function was causing the error.Ideally, the error message should also suggest something like