Printing formatted text in an Iced window

My goal is to print a simple math problem in a window. This is easy enough to do when working in the terminal. This code works fine:

fn main() {
    let num1 = 4;
    let num2 = 3;
    
    println!("\n{} x {} = ", num1, num2);
}

However, so far my attempts at printing that same line in an Iced window have failed. Here’s the code I’m using:

use iced::widget::text;
use iced::window;
use iced::{Element, Sandbox, Settings};

const PROGRAM_TITLE: &str = "Math Drill";
const VERSION: &str = "0.0.1";

struct Mathfact {
    num1: i32,
    num2: i32,
}

#[derive(Debug)]
enum Message {}

impl Sandbox for Mathfact {
    type Message = Message;

    fn new() -> Mathfact {
        let fact1 = Mathfact { num1: 4, num2: 3 };
        fact1
    }

    fn title(&self) -> String {
        String::from(PROGRAM_TITLE)
    }

    fn update(&mut self, _message: Self::Message) {
        // No interactions yet, so nothing to do here.
    }

    fn view(&self) -> Element<Self::Message> {
        //println!("\n{} x {} = ", self.num1, self.num2);
        "\n\nHello, world!".into()
    }
} // End Mathfact/Sandbox impl

pub fn main() -> iced::Result {
    Mathfact::run(Settings::default())
}

The problem, of course, is in the view() function. The formatted output with println() printed just fine – in the terminal, not the window. I had to include the

"\n\nHello, world!".into()

line to satisfy the return type of Element<Self::Message>. Admittedly, I don’t have a good understanding of what that return value is doing and, eventually, I need to get that figured out, but my goal is to get this text printed in the Iced window. How do I do that?

It turns out that return value is the mystery you need to solve. What do you see inside the window when you run your app?

“Hello, world!” in the upper left corner. My math problem shows up in the terminal.

The code is mostly the “Simplest Example” we were talking about in this thread.

Right, and what does your view code say?

I actually tried to find a way to insert the variables into the "Hello, world!" string, but finding a way to insert the variable values has me stumped. I’m also having trouble wrapping my brain around into(). I know it’s basically from() in reverse, but it’s still somewhat mysterious. (I think you talked a little about it in your video, but I’m kind of dense. :>)

Just tried this:

"\n{self.num1} x {self.num2} = ".into()

                       and

let output = format!("\n{} x {} = ", self.num1, self.num2);
output.into()

and of course, neither worked.

Great! Almost there! Try:

text(format!("{} x {} = ", self.num1, self.num2)).into()

into is used in this case to convert a specific type into a generic Element.

"Hello, world!" is a &'static str and can be turned into an Element with just into, which is convenient for cases such as button("Some text").

However, format! creates a String; therefore, into cannot be used directly. However, you can use the text helper to create a Text widget from any impl ToString. A Text widget can always be turned into an Element.

Awesome! That worked. Thank you.

Now I can start playing around with it. Tried to learn more about text(). Found Text in iced::advanced::widget - Rust but not text().