我有这个代码:
let routes = match env::var("ENV") {
Ok(el) => {
if el == "PROD" {
routes![upload_image]
} else {
routes![get_token, callback, form, upload_image, refresh]
}
},
_ => routes![get_token, callback, form, upload_image, refresh],
};
该函数env::var
返回一个Result<String, VarError>
. 我想知道我上面的代码是否可以简化如下:
let routes = match env::var("ENV") { // E: mismatched types: this expression has type `Result<std::string::String, VarError>
Ok("PROD") => { // E: mismatched types: expected `String`, found `&str`
routes![upload_image]
},
_ => routes![get_token, callback, form, upload_image, refresh],
};
String
但是,我收到有关“不匹配类型:预期,发现”的错误&str
。有没有办法简化这段代码?