ITADN

Language proposal: Return object construction in type

#3897Closedefikay 创建于 2025-12-21
E
efikaycommented
I'm not a good writer, so let's skip to examples right now. Before: ```rust 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: ```rust 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.
关闭于 2025-12-24 1 条评论