Rust + Iced: Problem with variable borrowing
⚓ Rust 📅 2026-03-13 👤 surdeus 👁️ 5I am trying to integrate Iced into my programming. I have written all the applicable piece parts including the view function. In the view function I have a column widget that contains 3 widgets, 2 buttons and 1 text. I have a called function (get_button_widget) that returns the button widget but have run into an error. The IDE is telling me that the text I am passing in is borrowed and as a result the IDE indicates that the container element I am returning from the view function cannot return a value referencing a temporary value. I think my misunderstanding is in one of two areas. 1) How to pass a variable as a reference to a function when Iced is involved or 2) The proper return type from the button function.
Any input will be appreciated. Below is the view function, the get_button_widget function and the get_text_widget function (called from the get_button_widget function).
fn view(&self) -> iced::Element<'_, Message> {
let label_size: f32 = 40.0;
let count_size: f32 = 50.0;
let plus_sign: String = "+".to_string();
let minus_sign: String = "-".to_string();
let column = widget::column![
get_button_widget(&plus_sign, label_size, Message::Increment),
// error - plus sign is borrowed here
widget::text(self.value.to_string()).size(count_size),
get_button_widget(&minus_sign, label_size, Message::Decrement),
// error - minus sign is borrowed here
].align_x(Horizontal::Center);
let mycontainer: Container<'_, Message, Theme, Renderer> =
widget::container(column)
.align_x(Horizontal::Center)
.align_y(Vertical::Center)
.width(Length::Fill)
.height(Length::Fill)
.style(container_style)
;
mycontainer.into()
// error - cannot return value referencing a temporary value
// error - returns a value referencing data owned by the current function
}
pub fn get_button_widget(label: &String, label_size: f32, on_press: Message) -> iced::widget::Button<'_, Message> {
button(get_text_widget(label, label_size))
.on_press(on_press)
.style(button_style)
.width(80)
.height(80)
}
pub fn get_text_widget(value: &String, size: f32) -> iced::widget::Text<'_> {
text(value).size(size).align_x(Horizontal::Center).align_y(Vertical::Center)
}
1 post - 1 participant
🏷️ Rust_feed