'Can't return a value referencing data owned by the current function
I am new to Rust, I just started learning it and wanted to build a simple GUI, and found this library iced has a great APi, but there is no resources to learn it and the documentation is not clear
I am trying to build two components, the first component is to show an article, and the second one to show a list of articles that wrap the first one
Here is my code
pub struct MarkData{
title: String,
description: String,
content: String,
image: String
}
enum Message{}
#[derive(Clone)]
pub struct MarkComponent{
data: MarkData
}
impl MarkComponent {
fn new(data: MarkData) -> Self {
Self {
data
}
}
pub fn view(&mut self) -> Row<Message> {
Row::new().push(
Text::new(&self.data.title).size(25)
)
}
}
#[derive(Clone)]
pub struct MarkComponents{
data: Vec<MarkComponent>,
}
impl MarkComponents {
pub fn new() -> Self {
let iter = (0..20).map(|a| MarkData {
title: format!("title {}", a),
description: format!("description {}", a),
content: format!("content {}", a),
image: format!("https://google.com")
});
Self {
data: Vec::from_iter(iter.map(|item| {
MarkComponent::new(item)
}))
}
}
pub fn view(&mut self) -> Row<Message> {
let mut row = Row::new();
for item in &self.data {
let content =
item.clone().view();
row = row.push( content ); // here is the error line
}
return row;
}
}
The problem is, when I try to return the create rwo from MarkComponent and push it to MarkComponents Row it panics and get this error:
returns a value referencing data owned by the current function
However when trying to push a Text element directly into row it works fine like the following
row = row.push(Text::new("now works fin!"));
but I want to created two components to separate things and start applying some design patterns
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
