这段代码:
extern crate rustc_serialize;
use rustc_serialize::json;
use rustc_serialize::json::Json;
fn main() {
let mut string: String = "{\"num\": 0}".to_string();
let mut obj = Json::from_str(&string).unwrap().as_object().unwrap();
let mut num = obj.get("num").unwrap().as_u64().unwrap();
for i in 1..10 {
println!("{:?}", num);
string = format!("{}{}{}", "{\"num\": ", i, "}");
obj = Json::from_str(&string).unwrap().as_object().unwrap();
num = obj.get("num").unwrap().as_u64().unwrap();
}
}
产生错误
error: borrowed value does not live long enough
--> src/main.rs:9:19
|>
9 |> let mut obj = Json::from_str(&string).unwrap().as_object().unwrap();
|> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ does not live long enough
note: reference must be valid for the block suffix following statement 1 at 9:72...
--> src/main.rs:9:73
|>
9 |> let mut obj = Json::from_str(&string).unwrap().as_object().unwrap();
|> ^
note: ...but borrowed value is only valid for the statement at 9:4
--> src/main.rs:9:5
|>
9 |> let mut obj = Json::from_str(&string).unwrap().as_object().unwrap();
|> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: consider using a `let` binding to increase its lifetime
--> src/main.rs:9:5
|>
9 |> let mut obj = Json::from_str(&string).unwrap().as_object().unwrap();
|> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: borrowed value does not live long enough
--> src/main.rs:16:15
|>
16 |> obj = Json::from_str(&string).unwrap().as_object().unwrap();
|> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ does not live long enough
note: reference must be valid for the block suffix following statement 1 at 9:72...
--> src/main.rs:9:73
|>
9 |> let mut obj = Json::from_str(&string).unwrap().as_object().unwrap();
|> ^
note: ...but borrowed value is only valid for the statement at 16:8
--> src/main.rs:16:9
|>
16 |> obj = Json::from_str(&string).unwrap().as_object().unwrap();
|> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: consider using a `let` binding to increase its lifetime
--> src/main.rs:16:9
|>
16 |> obj = Json::from_str(&string).unwrap().as_object().unwrap();
|> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
我不明白这是什么问题,错误是误导性的还是什么?为什么它指向 Json
- 这没有多大意义。此外,由于我的代码的性质,我无法在循环内创建另一个变量 - 必须重用旧变量。
最佳答案
问题是您使用 as_object
不正确。方法的名称(“as”,而不是“to”或“into”)表示它只代表 Json
对象作为另一种类型。 as_object
的返回类型是Option<&'a mut Object>
,它告诉我们它只返回一个与 Json
一样长的引用。对象本身。当你在单行中使用它时,借用检查器不高兴,因为中间 Json
此行后对象将被删除,但您尝试使用保留的引用。
一个解决方案是存储Json
在变量中并且根本不存储引用:
fn main() {
let mut string: String = "{\"num\": 0}".to_string();
let mut json = Json::from_str(&string).unwrap();
let mut num = json.as_object().unwrap().get("num").unwrap().as_u64().unwrap();
for i in 1..10 {
println!("{:?}", num);
string = format!("{}{}{}", "{\"num\": ", i, "}");
json = Json::from_str(&string).unwrap();
num = json.as_object().unwrap().get("num").unwrap().as_u64().unwrap();
}
}
如果您存储 as_object
的结果在变量中,Rust 不会让您重新分配 json
变量,因为它的内容是借用的。如果你真的想这样做,你需要将变量放在一个范围内,以便它在循环开始时不再可用。
关于while循环中借用的rustc_serialize::Json值不够活,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39296039/