我有这些 struct :
#[derive(Debug, RustcDecodable)]
struct Config {
ssl: Option<SslConfig>,
}
#[derive(Debug, RustcDecodable)]
struct SslConfig {
key: Option<String>,
cert: Option<String>,
}
他们从一个toml
人的档案中被填满.这个很好用.因为我得了Option<T>
分,所以我要么打unwrap()
,要么打match
.
但如果我想做以下事情:
let cfg: Config = read_config(); // Reads a File, parses it and returns the Config-Struct
let keypath = cfg.ssl.unwrap().key.unwrap();
let certpath = cfg.ssl.unwrap().cert.unwrap();
这行不通,因为cfg.ssl
会被转移到keypath
.但它为什么会被移动?我在ssl
上打unwrap()
来取 keys (然后unwrap()
把 keys 给我).所以key.unwrap()
的结果应该被移动?
还是我漏掉了一点?让这些 struct 像这样(或以其他简洁的方式)可访问的最佳方法是什么?我试图实现#[derive(Debug, RustcDecodable, Copy, Clone)]
,但这不起作用,因为我还必须实现Copy
到String
.然后我必须实现Copy
到Vec<u8>
等等.一定有更方便的解决办法吗?