Avatar ·

在Rust中是否有快速用默认值初始化结构体字段的方法?

你好!我开始学习Rust,在初始化结构体时遇到了问题。我有一个包含10个字段的结构体,需要为它们设置默认值。目前我是这样做的: ```rust struct Config { host: String, port: u16, timeout: u64, max_connections: u32, use_tls: bool, log_level: String, cache_size: usize, retry_count: u32, enable_metrics: bool, backup_url: String, } fn main() { let config = Config { host: String::from("localhost"), port: 8080, timeout: 30, max_connections: 100, use_tls: false, log_level: String::from("info"), cache_size: 1024, retry_count: 3, enable_metrics: true, backup_url: String::new(), }; } ``` 这代码太多了!在其他语言(比如C#或Java)中,我可以在结构体定义时直接声明字段并初始化。Rust中是否有某种语法糖或模式,可以不用写