Rust 错误[E0597]:尝试返回时,借用值的有效期不够长(&E)';静止的

Rust 错误[E0597]:尝试返回时,借用值的有效期不够长(&E)';静止的,rust,Rust,我正试图重写publicsuffix板条箱,希望获得更好的性能。我使用了同名的Python库作为参考,在简化了它的数据结构后,我得出了以下结论: #[derive(Debug)] pub struct Node<T> { pub name: T, pub children: Vec<Node<T>>, } 这无法编译,我不理解错误消息: error[E0597]:借入值的有效期不够长 -->src/main.rs:9:20 | 9 |子节点

我正试图重写
publicsuffix
板条箱,希望获得更好的性能。我使用了同名的Python库作为参考,在简化了它的数据结构后,我得出了以下结论:

#[derive(Debug)]
pub struct Node<T> {
    pub name: T,
    pub children: Vec<Node<T>>,
}

这无法编译,我不理解错误消息:

error[E0597]:借入值的有效期不够长
-->src/main.rs:9:20
|
9 |子节点:&[Node{
|  ____________________^
10 | |名称:“英国”,
11 | |儿童:&[
12 | |节点{
...  |
71 | |             ][..],
72 | |         }][..],
||uuuuuuuuuuuuuuuu^临时值的寿命不够长
73 |       };
|-临时价值仅在此之前有效
|
=注意:借用值必须在静态生存期内有效。。。
=注意:考虑使用“让”绑定来增加其生命周期。
错误[E0597]:借用值的有效期不足
-->src/main.rs:11:24
|
11 |儿童:&[
|  ________________________^
12 | |节点{
13 | |名称:“ac”,
14 | |儿童:&[…],
...  |
70 | |                 },
71 | |             ][..],
||uuuuuuuuuuuuuuuuuuuuuu^临时值的寿命不够长
72 |           }][..],
73 |       };
|-临时价值仅在此之前有效
|
=注意:借用值必须在静态生存期内有效。。。
=注意:考虑使用“让”绑定来增加其生命周期。
错误[E0597]:借用值的有效期不足
-->src/main.rs:18:32
|
18 |儿童:&[
|  ________________________________^
19 | |节点{
20 | |名称:“blogspot”,
21 | |儿童:&[…],
...  |
26 | |                         },
27 | |                     ][..],
||uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu^临时值的寿命不够长
...
73 |       };
|-临时价值仅在此之前有效
|
=注意:借用值必须在静态生存期内有效。。。
=注意:考虑使用“让”绑定来增加其生命周期。
错误[E0597]:借用值的有效期不足
-->src/main.rs:31:32
|
31 |子节点:&[Node{
|  ________________________________^
32 | |名称:“服务”,
33 | |儿童:&[…],
34 | |                     }][..],
||uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
...
73 |       };
|-临时价值仅在此之前有效
|
=注意:借用值必须在静态生存期内有效。。。
=注意:考虑使用“让”绑定来增加其生命周期。
错误[E0597]:借用值的有效期不足
-->src/main.rs:66:32
|
66 |子节点:&[Node{
|  ________________________________^
67 | |名称:“*”,
68 | |儿童:&[…],
69 | |                     }][..],
||uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
...
73 |       };
|-临时价值仅在此之前有效
|
=注意:借用值必须在静态生存期内有效。。。
=注意:考虑使用“让”绑定来增加其生命周期。

有没有办法解决这个问题?

创建切片后,请使用
&[][…]
再次引用它。这将创建一个临时对象,稍后将删除该对象。这是错误的来源

当您从代码中删除所有
[…]
时,您可以修复它

#[derive(Debug)]
pub struct Node<T: 'static> {
    pub name: T,
    pub children: &'static [Node<T>],
}
pub fn return_root() -> Node<&'static str> {
    return Node {
        name: "root",
        children: &[Node {
            name: "uk",
            children: &[
                Node {
                    name: "ac",
                    children: &[],
                },
                Node {
                    name: "co",
                    children: &[
                        Node {
                            name: "blogspot",
                            children: &[],
                        },
                        Node {
                            name: "no-ip",
                            children: &[],
                        },
                    ],
                },
                Node {
                    name: "gov",
                    children: &[Node {
                        name: "service",
                        children: &[],
                    }],
                },
                Node {
                    name: "ltd",
                    children: &[],
                },
                Node {
                    name: "me",
                    children: &[],
                },
                Node {
                    name: "net",
                    children: &[],
                },
                Node {
                    name: "nhs",
                    children: &[],
                },
                Node {
                    name: "org",
                    children: &[],
                },
                Node {
                    name: "plc",
                    children: &[],
                },
                Node {
                    name: "police",
                    children: &[],
                },
                Node {
                    name: "sch",
                    children: &[Node {
                        name: "*",
                        children: &[],
                    }],
                },
            ],
        }],
    };
}
#[派生(调试)]
发布结构节点
#[derive(Debug)]
pub struct Node<T: 'static> {
    pub name: T,
    pub children: &'static [Node<T>],
}
pub fn return_root() -> Node<&'static str> {
    return Node {
        name: "root",
        children: &[Node {
            name: "uk",
            children: &[
                Node {
                    name: "ac",
                    children: &[],
                },
                Node {
                    name: "co",
                    children: &[
                        Node {
                            name: "blogspot",
                            children: &[],
                        },
                        Node {
                            name: "no-ip",
                            children: &[],
                        },
                    ],
                },
                Node {
                    name: "gov",
                    children: &[Node {
                        name: "service",
                        children: &[],
                    }],
                },
                Node {
                    name: "ltd",
                    children: &[],
                },
                Node {
                    name: "me",
                    children: &[],
                },
                Node {
                    name: "net",
                    children: &[],
                },
                Node {
                    name: "nhs",
                    children: &[],
                },
                Node {
                    name: "org",
                    children: &[],
                },
                Node {
                    name: "plc",
                    children: &[],
                },
                Node {
                    name: "police",
                    children: &[],
                },
                Node {
                    name: "sch",
                    children: &[Node {
                        name: "*",
                        children: &[],
                    }],
                },
            ],
        }],
    };
}