Hacker News new | ask | show | jobs
by BetterThanSober 12 days ago
> fn update(_: &mut (), _: ()) {}

> move |_| container::background(c)

There's no way anyone can see this and thinks it is legible

6 comments

Looks pretty clear to me and I’ve not written a line of Rust in my life.

New languages always feel a bit inapproachable. It’s like learning Russian (for a westerner). Super impenetrable at first, but at some point (early on!) the alphabet just clicks and then the rest is “just” regular language learning. Same with PL syntax. Overwhelming at first, until it clicks and then you can’t remember what you found overwhelming about it.

The former is just a shortcut taken for the specific example - types are omitted because state modification is missing

The latter is literally just a normal closure. Sure, you get a move which is an ownership semantic, but otherwise it's a very normal closure with an unused parameter, writing

  (_) => container::background(c)
doesn't make it much better, does it?
> fn update(_: &mut ALongAndBoringTypeName, _: ALongAndBoringTypeName) {}

Does it make more sense now?

fn update isn't being used so those are placeholders.
Your example would read far easier on the eyes if you just set the state to be some generic blank struct (e.g `AppState`) than the tedious syntax of `()`.

    use iced::widget::{column, *};
    use iced::{Center, Color, Fill, Theme, border, color};

    fn main() -> iced::Result {
        iced::application(new, update, view)
            .window_size((500, 500))
            .run()
    }

    // fill in your state and message types as needed
    pub type State = ();
    pub type Message = ();

    fn new() -> State {}

    fn update(state: &mut State, message: Message) {}

    fn view(state: &State) -> impl Widget<Message> {
        let swatch = |c: Color| {
            container("")
                .width(32)
                .height(32)
                .style(move |theme| container::background(c))
        };
        let colors = [
            color!(0xff0000),
            color!(0x00ff00),
            color!(0x0000ff),
            color!(0xffff00),
            Color::BLACK,
            Color::WHITE,
        ];

        container(
            column![
                text("Hello, World!").size(20).color(Color::WHITE),
                row(colors.map(swatch)).spacing(8),
            ]
            .spacing(12)
            .align_x(Center),
        )
        .center(Fill)
        .style(panel)
    }

    fn panel(theme: &Theme) -> container::Style {
        container::background(color!(0x505050)).border(border::color(color!(0x0000ff)).width(1))
    }

but you'll get warnings for unused variables
> but you'll get warnings for unused variables

Yeah, that's fine - they're clearly placeholders lol

Are you old enough to remember Perl hackers? Some of those guys might ;)

I really like :()), it looks like a monkey with two chins!

> There's no way anyone can see this and thinks it is legible

I see this and think it is very legible. It is hard to imagine why anyone can have issues with this.