# rust_opt_param **Repository Path**: omyscode/rust_opt_param ## Basic Information - **Project Name**: rust_opt_param - **Description**: 通过闭包实现更优雅的方式来实现rust私有字段可选参数的设置 - **Primary Language**: Rust - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-03-03 - **Last Updated**: 2022-03-03 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 在Rust中用更好的方式设置私有字段的参数 ## 项目基于Rust闭包实现,让你可以通过如下形式链式调用(因为Rust没有默认的可变参数实现,如有需求可将相关setter方法放入vec中,也可实现功能)`configure`方法来设置私有字段参数 ```rust fn main() { let mut app = Application::default(); println!("{:?}", app); // output: Application { name: "", version: V1 } app.configure( Application::set_name("set by opt") ) .configure( Application::set_version(version::Version::V2) ); println!("{:?}", app); // output: Application { name: "set by opt", version: V2 } } ``` ## 优点 - 可有效保护(限制)参数值 - 相比于直接通过`.field`设置字段值更优雅