我正在学习Rust以及extra :: json模块.这是我的示例(带有额外的不需要的类型注释):
let j:Result<Json,JsonError> = from_str("[{\"bar\":\"baz\", \"biz\":123}]");
let l:List = match j {
Ok(List(l)) => l,
Ok(_) => fail!("Expected a list at the top level"),
Err(e) => fail!(fmt!("Error: %?", e))
};
println(fmt!("item = %?", l.iter().advance(|i|{
match i {
&Object(o) => {
println(fmt!("Object is %?", o));
},
_ => {
fail!("Should be a list of objects, no?");
}
}
println(fmt!("i=%?", i));
true
})));
Run Code Online (Sandbox Code Playgroud)
当我编译时,我得到这个:
$ rust run json.rs
json.rs:70:9: 70:18 error: cannot move out of dereference of & pointer
json.rs:70 &Object(o) => {
^~~~~~~~~
note: in expansion of fmt!
json.rs:68:10: 79:6 note: expansion site
error: aborting due to previous error
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
Cor*_*son 11
像这样的模式是解构,这意味着他们默认移出他们匹配的东西.你要:
&Object(ref o) => { ... }
Run Code Online (Sandbox Code Playgroud)
这需要借用成员的参考,而不是移出它.
| 归档时间: |
|
| 查看次数: |
1518 次 |
| 最近记录: |