Skip to content

Language proposal: Return object construction in type #3897

@efikay

Description

@efikay

I'm not a good writer, so let's skip to examples right now.

Before:

mod rfc {
    type Size = (usize, usize);

    #[derive(Default)]
    struct Window {
        pub title: String,
        pub size: Size,
        pub close_when_requested: bool,
    }

    // ⚙️ Just regular Rust, nothing inconvenient
    fn make_diagnostic_window(size: Size) -> Window {
        Window {
            size,
            title: "Diagnostic window".into(),
            ..Default::default()
        }
    }
}

After:

mod rfc {
    type Size = (usize, usize);

    #[derive(Default)]
    struct Window {
        pub title: String,
        pub size: Size,
        pub close_when_requested: bool,
    }

    // ⚡️ Shorthand proposal
    fn make_diagnostic_window(size: Size) => Window {
        size,
        title: "Diagnostic window".into(),
        ..Default::default()
    }

    // Still an option
    fn old_fashioned_way(size: Size) -> Window {
        Window {
            size,
            title: "Diagnostic window".into(),
            ..Default::default()
        }
    }
}

As average language user, I'm not sure about all possible caveats but in my opinion this syntax looks appropriate enough. Simple, clean, fast to write & maintain.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions