Rust 使用宏时发生借用错误
我试图使用我发现有问题的代码,基本上这是一个借用错误,使用了一些宏,错误:Rust 使用宏时发生借用错误,rust,Rust,我试图使用我发现有问题的代码,基本上这是一个借用错误,使用了一些宏,错误: Compiling playground v0.0.1 (file:///playground) error[E0597]: `body` does not live long enough --> src/main.rs:12:3 | 6 | let raw_structure = borrow_function(&body); |
Compiling playground v0.0.1 (file:///playground)
error[E0597]: `body` does not live long enough
--> src/main.rs:12:3
|
6 | let raw_structure = borrow_function(&body);
| ---- borrow occurs here
...
12 | }
| ^ `body` dropped here while still borrowed
...
31 | let body = get_body_as!(&str, "Hello", function1);
| -------------------------------------- in this macro invocation
32 | println!("Hello");
33 | }
| - borrowed value needs to live until here
我设法创建了一个最小的、完整的、可验证的示例,我认为解决方案是将宏转换为函数,但我也不完全确定如何做到这一点(
):
macro\u规则!得到你的身体{
($structure:ty,$req:expr,$error\u fn:ident)=>{
{
let body=get_body!($req,$error_fn);
让原始结构=借用函数(&body);
匹配原始结构{
Ok(结构)=>结构,
Err(error)=>“error”
}
}
}
}
宏规则!得到你的身体{
($req:expr,$error\u fn:ident)=>{
{
让mut payload=String::new();
有效载荷
}
}
}
fn借用函数(s:&str)->结果{
好(s)
}
fn main(){
设function1=|s:&str|s;
let body=get_body_as!(&str,“Hello”,function1);
println!(“你好”);
}
问题在于,您正试图从拥有body
变量的块返回对body
变量的引用,但body
将被丢弃在该块的末尾,因此该引用将比它引用的数据更有效
如果希望编译示例,可以更改代码,以便在main
函数中声明body
,使用添加到get_body\u as
宏的ident参数:
macro_rules! get_body_as {
($structure:ty, $req:expr, $error_fn:ident, $body: ident) => {
let $body = get_body!($req, $error_fn);
let raw_structure = borrow_function(&$body);
match raw_structure {
Ok(structure) => structure,
Err(error) => "Error"
}
}
}
macro_rules! get_body {
($req:expr, $error_fn:ident) => {
{
let mut payload = String::new();
payload
}
}
}
fn borrow_function(s: &str) -> Result<&str, &str> {
Ok(s)
}
fn main() {
let function1 = |s: &str| s;
get_body_as!(&str, "Hello", function1, body);
println!("Hello");
}
macro\u规则!得到你的身体{
($structure:ty,$req:expr,$error\u fn:ident,$body:ident)=>{
让$body=get_body!($req,$error_fn);
让原始结构=借用函数(&$body);
匹配原始结构{
Ok(结构)=>结构,
Err(error)=>“error”
}
}
}
宏规则!得到你的身体{
($req:expr,$error\u fn:ident)=>{
{
让mut payload=String::new();
有效载荷
}
}
}
fn借用函数(s:&str)->结果{
好(s)
}
fn main(){
设function1=|s:&str|s;
get_body_as!(&str,“Hello”,function1,body);
println!(“你好”);
}
此示例已编译,但仍有关于未使用变量的警告,我仅对编译进行了最小的更改以使编译成功。请在问题本身中发布一个包含代码相关部分的最小示例。请看。@interjay我是一个生锈的新手,这个项目使用了很多板条箱,所以要创建一个最小的完整的和可验证的示例对我来说并不容易,我将尝试重新制定question@interjay我已经成功创建了一个最小的、完整的和可验证的示例(我认为这符合规则),任何帮助都将不胜感激。谢谢,最后,它对我不起作用,因为用例比示例更复杂,但是你让我更好地理解了宏如何在Rust中工作,给了你正确的答案,因为它解决了操场上的问题。
macro_rules! get_body_as {
($structure:ty, $req:expr, $error_fn:ident, $body: ident) => {
let $body = get_body!($req, $error_fn);
let raw_structure = borrow_function(&$body);
match raw_structure {
Ok(structure) => structure,
Err(error) => "Error"
}
}
}
macro_rules! get_body {
($req:expr, $error_fn:ident) => {
{
let mut payload = String::new();
payload
}
}
}
fn borrow_function(s: &str) -> Result<&str, &str> {
Ok(s)
}
fn main() {
let function1 = |s: &str| s;
get_body_as!(&str, "Hello", function1, body);
println!("Hello");
}