logoalt Hacker News

flohofwoelast Monday at 9:17 AM1 replyview on HN

> Surely Rust can infer the type in your example?

Well in Rust code like this:

    pass_action.colors[0] = sg::ColorAttachmentAction {
        load_action: sg::LoadAction::Clear,
        clear_value: sg::Color { r: 0.25, g: 0.5, b: 0.75, a: 1.0 },
        ..Default::default()
    };
...I cannot write:

    pass_action.colors[0] = {
        load_action: sg::LoadAction::Clear,
        clear_value: { r: 0.25, g: 0.5, b: 0.75, a: 1.0 },
        ..Default::default()
    };
...even though the Rust compiler has all the type information it needs (from the 'left-hand-side').

For comparison, in Zig it would look like this:

    pass_action.colors[0] = .{
        .load_action = .Clear,
        .clear_value = .{ .r=0.25, .g=0.5, .b=0.75, .a=1.0 },
    };
...Zig is still only halfway there compared to C99 (e.g. Zig doesn't allow designator chaining and is much less flexible for initializing nested arrays - in those areas it's closer to Rust than C).

Replies

tialaramexlast Monday at 9:55 AM

Right but your call to Default::default() gives the game away that we do have inference. Default::default() is generic, if we didn't have inference we'd need to tell it which of the enormous number of implementations of that trait it should call.

Would you take syntax like clear_value: _ { r: 0.25, g: 0.5, g: 0.75, a: 1.0 } ?? Then we're saying that we know we need to pick a type here but the type can be inferred where our underscore was, just like when we

let words: Vec<_> = "A sentence broken by spaces".split_whitespace().collect();