Please see the code segment below with comments:
#![feature(in_band_lifetimes)]
use std::borrow::Cow;
/* if input is borrowed, then return a Cow reference to its first two chars. Pls do not allocate memory.
* if input is owned, then keep only the first two chars and discard everything else. Pls modify in-place.
*/
fn test(input: Cow<'a, str>)-> Cow<'a, str>{
Cow::from(&input[0..2])
}
And the error output is:
error[E0515]: cannot return value referencing function parameter `input`
--> src/main.rs:8:5
|
8 | Cow::from(&input[0..2])
| ^^^^^^^^^^^-----^^^^^^^
| | |
| | `input` is borrowed here
| returns a value referencing data owned by the current function
I must have misunderstood some point of the principle of either the borrow checker or Cow of Rust. Can anyone guide me through this pls?