rust/tests/ui/lifetimes/temporary-lifetime-extension.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

34 lines
677 B
Rust
Raw Normal View History

2024-02-20 14:19:16 +00:00
//@ check-pass
fn temp() -> (String, i32) {
(String::from("Hello"), 1)
}
fn main() {
let a = &temp();
let b = [(&temp(),)];
let c = &temp().0;
let d = &temp().0[..];
let e = {
let _ = 123;
&(*temp().0)[..]
};
let f = if true {
&temp()
} else {
&temp()
};
let g = match true {
true => &temp(),
false => {
let _ = 123;
&temp()
}
};
let h = match temp() {
2024-02-20 15:23:05 +00:00
// The {} moves the value, making a new temporary.
owned_non_temporary => &{ owned_non_temporary },
};
println!("{a:?} {b:?} {c:?} {d:?} {e:?} {f:?} {g:?} {h:?}");
2024-02-20 14:19:16 +00:00
}