admin管理员组

文章数量:1391964

I have a function which matches against an enum with Strings and makes text elements with a color corresponding to the enum. I want to store those Texts 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 Strings and makes text elements with a color corresponding to the enum. I want to store those Texts 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
  • Not sure I understand what that last error is coming from – kmdreko Commented Mar 13 at 3:29
  • 1 That's because the code in the question doesn't reproduce the error. OP please edit your question to include a minimal reproducible example, the code you show has several semantic and syntax errors unrelated to your problem (use without ;, fn text being shadowed multiple times, &mut self without an enclosing impl block, …). Once I resolved them and added a lifetime to Text in MyApp this compiles. – cafce25 Commented Mar 13 at 8:31
  • Sorry didn't check the example. The new one still gives me the 'Debug not implemented' error even after fixing the syntax errors. – NunjaUwU Commented Mar 15 at 14:06
Add a comment  | 

1 Answer 1

Reset to default 1

Widgets 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