相关疑难解决方法(0)

<'a,'b:'a>是否意味着生命周期'b必须比生命周期更长'a?

我想实现一个类似于标准库定义的调试构建器的构建器.它们使用如下结构定义:

struct DebugFoo<'a, 'b: 'a> {
    fmt: &'a mut std::fmt::Formatter<'b>
}
Run Code Online (Sandbox Code Playgroud)

既然我不明白表格的<'a, 'b: 'a>含义,也不能在Rust书或Rust参考文献中找到它(至少关于生命周期),我只是试图删除我不明白的内容,看看会发生什么:

struct DebugFoo<'a, 'b> {
    fmt: &'a mut std::fmt::Formatter<'b>
}
Run Code Online (Sandbox Code Playgroud)

编译它我得到这个错误:

in type `&'a mut core::fmt::Formatter<'b>`, reference has a longer 
lifetime than the data it references
Run Code Online (Sandbox Code Playgroud)

而且这个说明:

the pointer is valid for the lifetime 'a as defined on the struct at 1:0
but the referenced data is only valid for the lifetime 'b as defined on
the struct at 1:0
Run Code Online (Sandbox Code Playgroud)

这对我来说是有道理的:'a并且 …

lifetime rust

16
推荐指数
2
解决办法
2024
查看次数

标签 统计

lifetime ×1

rust ×1