I have the following struct:
struct CompareUrl {
repo: String,
left: String,
right: String,
}
And I want this struct to return itself in bytes.
impl CompareUrl {
pub fn as_bytes(&self) -> &[u8] {
format!(
"https://github.com/{}/compare/{}...{}",
self.repo,
self.left,
self.right
).as_bytes()
}
}
But it complains about ownership:
cannot return reference to temporary value
returns a reference to data owned by the current function
It seems that it works for (&str).as_bytes(), but not for (String).as_bytes(). My attempt was to solve this like that:
impl CompareUrl {
pub fn as_bytes(&self) -> &[u8] {
format!(
"https://github.com/{}/compare/{}...{}",
self.repo,
self.left,
self.right
).as_str().as_bytes()
}
}
But it raises the same error. How can I solve it?