admin管理员组文章数量:1391964
I have a function which matches against an enum with String
s and makes text elements with a color corresponding to the enum. I want to store those Text
s in a field of MyApp
.
(Edit) The previous example was full of syntax errors and it was missing a few important functions, the new example gives the error message described at the bottom but the solution pirate suggested is way better.
specifying the lifetime removed the first error.
New example
fn main() -> iced::Result {
iced::run("", MyApp::update, MyApp::view)
}
use iced::{
widget::{text, text::Text},
Color, Element,
};
#[derive(Default, Debug)]
struct MyApp<'a> {
texts: Vec<Text<'a>>,
}
enum TextType {
A(String),
B(String),
}
enum Message {}
impl<'a> MyApp<'a> {
fn view(&self) -> Element<Message> {
todo!()
}
fn update(&mut self, message: Message) {
todo!()
}
fn color_text(&mut self, texts: Vec<TextType>) {
let mut collor_text = Vec::new();
for text in texts {
match text {
TextType::A(txt) => collor_text.push(text!("{}", txt).color(Color::WHITE)),
TextType::B(txt) => collor_text.push(text!("{}", txt).color(Color::BLACK)),
}
}
self.texts = collor_text;
}
}
(Old example) Something like this:
use iced::{Color, widget::{text, Text}}
struct MyApp {
texts: Vec<Text>
}
enum TextType {
A(String),
B(String)
}
fn color_text(&mut self, texts: Vec<TextType>) {
let mut collor_text = Vec::new();
for text in texts {
match text {
A(text) => collor_text.push(text(text).color(Color::WHITE)),
B(text) => collor_text.push(text(text).color(Color::BLACK))
}
}
self.texts = collor_text;
}
If I try it with Vec<Text>
the compiler tells me expected named lifetime parameter
. When I try to use a lifetime like 'a
the compiler says
iced::advanced::widget::Text<'static, Theme, iced_renderer::fallback::Renderer<iced_wgpu::Renderer, iced_tiny_skia::Renderer>>` cannot be formatted using `{:?}` because it doesn't implement `Debug
Is there a way to declare the type or a better solution?
I have a function which matches against an enum with String
s and makes text elements with a color corresponding to the enum. I want to store those Text
s in a field of MyApp
.
(Edit) The previous example was full of syntax errors and it was missing a few important functions, the new example gives the error message described at the bottom but the solution pirate suggested is way better.
specifying the lifetime removed the first error.
New example
fn main() -> iced::Result {
iced::run("", MyApp::update, MyApp::view)
}
use iced::{
widget::{text, text::Text},
Color, Element,
};
#[derive(Default, Debug)]
struct MyApp<'a> {
texts: Vec<Text<'a>>,
}
enum TextType {
A(String),
B(String),
}
enum Message {}
impl<'a> MyApp<'a> {
fn view(&self) -> Element<Message> {
todo!()
}
fn update(&mut self, message: Message) {
todo!()
}
fn color_text(&mut self, texts: Vec<TextType>) {
let mut collor_text = Vec::new();
for text in texts {
match text {
TextType::A(txt) => collor_text.push(text!("{}", txt).color(Color::WHITE)),
TextType::B(txt) => collor_text.push(text!("{}", txt).color(Color::BLACK)),
}
}
self.texts = collor_text;
}
}
(Old example) Something like this:
use iced::{Color, widget::{text, Text}}
struct MyApp {
texts: Vec<Text>
}
enum TextType {
A(String),
B(String)
}
fn color_text(&mut self, texts: Vec<TextType>) {
let mut collor_text = Vec::new();
for text in texts {
match text {
A(text) => collor_text.push(text(text).color(Color::WHITE)),
B(text) => collor_text.push(text(text).color(Color::BLACK))
}
}
self.texts = collor_text;
}
If I try it with Vec<Text>
the compiler tells me expected named lifetime parameter
. When I try to use a lifetime like 'a
the compiler says
iced::advanced::widget::Text<'static, Theme, iced_renderer::fallback::Renderer<iced_wgpu::Renderer, iced_tiny_skia::Renderer>>` cannot be formatted using `{:?}` because it doesn't implement `Debug
Is there a way to declare the type or a better solution?
Share Improve this question edited Mar 15 at 13:49 NunjaUwU asked Mar 12 at 18:44 NunjaUwUNunjaUwU 277 bronze badges 3 |1 Answer
Reset to default 1Widgets are not meant to be stored as struct Member. Instead you should store it's underlying data. Example, you can store both texts and their colors as Vec<(String, Color)>
, then use that underlying data to create Widgets
in fn view()
you might be able to store it as struct memeber, if you supply all the generics and lifetime like Message
, Theme
, Renderer
and more But not worth the hassle.
本文标签: rustStore iced Element of Text in my Apps structStack Overflow
版权声明:本文标题:rust - Store iced Element of Text in my Apps struct - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744733876a2622208.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
use
without;
,fn text
being shadowed multiple times,&mut self
without an enclosingimpl
block, …). Once I resolved them and added a lifetime toText
inMyApp
this compiles. – cafce25 Commented Mar 13 at 8:31