cuviper's input.first().copied()
is a good answer to the original question here.
I wanted to address something that wasn't asked, since it's a common pitfall.
That general &'a &'a
-like pattern is usually not what you want, since it ties more lifetimes together.
Specifically here it means that the lifetime you'll return is the shorter of the lifetime of the items and the lifetime of the slice. But there's no reason for the return value to care about the lifetime of the slice, since it has pulled out an item.
Here's a demo of this mattering:
fn first_opt<'a>(input: &'a [&'a str]) -> Option<&'a str> {
input.first().copied()
}
fn demo() -> Option<&'static str> {
let a = ["hello", "world"];
first_opt(&a)
}
Compiling that, we get
error[E0515]: cannot return value referencing local variable `a`
--> src/lib.rs:7:5
|
7 | first_opt(&a)
| ^^^^^^^^^^--^
| | |
| | `a` is borrowed here
| returns a value referencing data owned by the current function
Where the compiler is "correct" because of what we said in the first_opt
signature, but that's not what we wanted to happen here -- those string literals are &'static str
, so could be returned just fine.
If we change it to have a different lifetime for the slice,
fn first_opt<'slice, 'a>(input: &'slice [&'a str]) -> Option<&'a str> {
input.first().copied()
}
then it compiles fine.
(That has a named lifetime for emphasis, but outside a pedagogical context I'd write it as just
fn first_opt<'a>(input: &[&'a str]) -> Option<&'a str> {
input.first().copied()
}
naming only the lifetime that needs to be mentioned more than once to tie things together.)