错误[E0597]:尝试返回&'static时,借入的值寿命不足

时间:2018-06-30 10:26:19

标签: rust

我正在尝试重写publicsuffix板条箱,以期获得更好的性能。我正在使用与参考相同名称的Python库,并且在简化了它的数据结构之后,我想到了:

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

在收到结构加载良好的信号之前,我感到太懒了,无法实现结构加载,这就是为什么我决定对其进行硬编码的原因。当我尝试进行类似error[E0015]: calls in statics are limited to constant functions, struct and enum constructors的操作时,我会不断得到pub static root = Node { name: "x", children: vec![]};,所以我决定将其包装在一个函数中。我最终遇到this file,这需要花很长时间才能完成(大约10分钟)。在问过IRC#rust-beginners之后,我得到了一个建议,尝试尝试使用对数组切片的引用,并最终得到以下示例代码:

#[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: &[][..],
                    }][..],
                },
            ][..],
        }][..],
    };
}

playground

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

error[E0597]: borrowed value does not live long enough
  --> src/main.rs:9:20
   |
9  |           children: &[Node {
   |  ____________________^
10 | |             name: "uk",
11 | |             children: &[
12 | |                 Node {
...  |
71 | |             ][..],
72 | |         }][..],
   | |__________^ temporary value does not live long enough
73 |       };
   |        - temporary value only lives until here
   |
   = note: borrowed value must be valid for the static lifetime...
   = note: consider using a `let` binding to increase its lifetime

error[E0597]: borrowed value does not live long enough
  --> src/main.rs:11:24
   |
11 |               children: &[
   |  ________________________^
12 | |                 Node {
13 | |                     name: "ac",
14 | |                     children: &[][..],
...  |
70 | |                 },
71 | |             ][..],
   | |_____________^ temporary value does not live long enough
72 |           }][..],
73 |       };
   |        - temporary value only lives until here
   |
   = note: borrowed value must be valid for the static lifetime...
   = note: consider using a `let` binding to increase its lifetime

error[E0597]: borrowed value does not live long enough
  --> src/main.rs:18:32
   |
18 |                       children: &[
   |  ________________________________^
19 | |                         Node {
20 | |                             name: "blogspot",
21 | |                             children: &[][..],
...  |
26 | |                         },
27 | |                     ][..],
   | |_____________________^ temporary value does not live long enough
...
73 |       };
   |        - temporary value only lives until here
   |
   = note: borrowed value must be valid for the static lifetime...
   = note: consider using a `let` binding to increase its lifetime

error[E0597]: borrowed value does not live long enough
  --> src/main.rs:31:32
   |
31 |                       children: &[Node {
   |  ________________________________^
32 | |                         name: "service",
33 | |                         children: &[][..],
34 | |                     }][..],
   | |______________________^ temporary value does not live long enough
...
73 |       };
   |        - temporary value only lives until here
   |
   = note: borrowed value must be valid for the static lifetime...
   = note: consider using a `let` binding to increase its lifetime

error[E0597]: borrowed value does not live long enough
  --> src/main.rs:66:32
   |
66 |                       children: &[Node {
   |  ________________________________^
67 | |                         name: "*",
68 | |                         children: &[][..],
69 | |                     }][..],
   | |______________________^ temporary value does not live long enough
...
73 |       };
   |        - temporary value only lives until here
   |
   = note: borrowed value must be valid for the static lifetime...
   = note: consider using a `let` binding to increase its lifetime

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

1 个答案:

答案 0 :(得分:1)

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

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

#[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: &[],
                    }],
                },
            ],
        }],
    };
}

playground

不需要[..],因为它仅从另一个切片创建一个切片,从而产生相同的切片。