這不完全是一個慣用的MVCE,但它應該說明問題。考慮下面的代碼:爲什麼我不能參考嘗試的結果!與&?
fn foo() -> Result<String, i32> {
return Ok("world".to_string());
}
fn bar() -> Result<String, i32> {
let mut value = String::new();
value.push_str(&try!(foo())); // this line here
return Ok("Hello ".to_string() + &value);
}
fn main() {
match bar() {
Ok(message) => println!("{}", message),
_ => return,
}
}
鏽返回錯誤:
<std macros>:3:43: 3:46
error: mismatched types:
expectedstr
,
foundcollections::string::String
(expectedstr
,
found structcollections::string::String
) [E0308]
<std macros>:3 $ crate:: result:: Result:: Ok (val) => val , $ crate:: result:: Result::
<std macros>:1:1: 6:48
note: in expansion oftry!
<std macros>:3:43: 3:46
help: runrustc --explain E0308
to see a detailed explanation
error: aborting due to previous error
如果我不是捕捉try!
結果,並分別適用&
的結果,它的工作原理(並打印出Hello world
):
fn foo() -> Result<String, i32> {
return Ok("world".to_string());
}
fn bar() -> Result<String, i32> {
let mut value = String::new();
let foo_result = try!(foo()); // capture the result of try!
value.push_str(&foo_result); // now use the result with &
return Ok("Hello ".to_string() + &value);
}
fn main() {
match bar() {
Ok(message) => println!("{}", message),
_ => return,
}
}
爲什麼let foo_result = try!(foo()); value.push_str(&foo_result);
工作但value.push_str(&try!(foo()));
不?從我的天真的角度來看,它們看起來是等價的,所以我不確定Rust的關鍵部分我不理解。
這是一個非常好的MVCE。不是答案,但仍然是我通常所做的一些事情:http://is.gd/PZKWz0 – ArtemGr
即使使用value.push_str(&& try!(foo()))也可以解決問題。 – eulerdisk
謝謝@Shepmaster。你是完全正確的。 – Cornstalks