I have the following code:
use std::borrow::Cow;
use std::ops::Range;
pub struct F {
s: String,
}
impl F {
pub fn get_content(&self) -> Cow<str> {
Cow::Borrowed(&self.s)
}
pub fn get_span(&self, span: Range<usize>) -> Option<Cow<str>> {
self.get_content().get(span).map(|s| Cow::Borrowed(s))
}
}
This, of course, raises a borrow error:
error[E0515]: cannot return value referencing temporary value
--> <source>:14:9
|
14 | self.get_content().get(span).map(|s| Cow::Borrowed(s))
| ------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| returns a value referencing data owned by the current function
| temporary value created here
error: aborting due to previous error
Even with explicit lifetimes it won't compile.
In my understanding this should be safe and possible, because get_content
is ties to self
, but get_span
is tied to self
as well, and calling &'a self
should return a Cow<'a, str>
, doesn't it?
Is there any way to achieve this in safe rust?