rust - 如果我在代码的不同位置使用非可变变量,为什么会出现借用错误?
问题描述
我正在学习 rust,我正在努力理解为什么以下代码无法编译,并在 req.into_body() 上出现一些“错误 [E0505]:无法移出,req
因为它是借用的”错误。如果我删除 println! 或在比赛前移动它,它会运行。
async fn hello_world(req: Request<Body>) -> Result<Response<Body>, Infallible> {
let mut response = Response::new(Body::empty());
let method = req.method();
let path = req.uri().path();
match (method, path) {
(&Method::GET, "/") => {
*response.body_mut() = Body::from("Try POSTing data to /echo");
}
(&Method::POST, "/echo") => {
*response.body_mut() = req.into_body();
}
_ => {
*response.status_mut() = StatusCode::NOT_FOUND;
}
};
println!("{} {}", method, path);
Ok(response)
}
我理解一些借用,但我不明白为什么 println 的位置会发生任何变化,因为我已经定义了在匹配中成功使用的非可变变量。
解决方案
您不能同时拥有可变和不可变借用。对于您的情况,将借用类型(method()
和的返回uri().path()
)转换为拥有的类型。您可以通过ToOwned::to_owned
或通过显式转换(操场)来做到这一点:
async fn hello_world(req: Request<Body>) -> Result<Response<Body>, Infallible> {
let mut response = Response::new(Body::empty());
let method = req.method().clone(); // &Method -> Method
let path = req.uri().path().to_string(); // &str -> String
// `req` is not borrowed at this point, so it can be mutated
match (method.clone(), path.as_str()) {
(Method::GET, "/") => {
*response.body_mut() = Body::from("Try POSTing data to /echo");
}
(Method::POST, "/echo") => {
*response.body_mut() = req.into_body();
}
_ => {
*response.status_mut() = StatusCode::NOT_FOUND;
}
};
println!("{} {}", method, path);
Ok(response)
}
推荐阅读
- javascript - 单击停止按钮时,我想显示五彩纸屑。单击停止按钮时如何在整个页面上显示五彩纸屑
- reactjs - 从 redux-saga 更改路由时突出显示“antd”菜单项
- android - 从 logcat Android-TV 中提取命令
- datastage - IBM Datastage 假设列是 WVARCHAR 而它是日期
- javascript - Bootstrap 3:如何通过单击当前导航栏内容中的超链接来链接到另一个导航栏?
- python - 斐波那契数列递归法
- ckeditor - 如何理解CKEditor4的“justify”选项?
- python - 使用顶层分类层对 Inception V3 层进行预训练
- c++ - 我们如何使用 AWS SDK C++ 在 cloudwatch 中查询已经创建的“LogGroup”和“StreamName”?
- openflow - Ryu 控制器 - simple_switch_13_stp 在失败后不重新计算树?