我希望实现一个类似于标准库定义的调试 构建器 的构建器。它们使用类似以下结构定义:
struct DebugFoo<'a, 'b: 'a> {
fmt: &'a mut std::fmt::Formatter<'b>
}
由于我不理解表单<'a, 'b: 'a>
的含义,也无法在Rust书或Rust参考资料中找到相关内容(至少关于生命周期),因此我尝试删除我不理解的部分,以查看会发生什么:
struct DebugFoo<'a, 'b> {
fmt: &'a mut std::fmt::Formatter<'b>
}
编译时出现以下错误:
in type `&'a mut core::fmt::Formatter<'b>`, reference has a longer
lifetime than the data it references
还有这个注释:
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
我理解:'a和'b是不同的生命周期,因此为了安全起见,Rust(借用检查器?)假定'a将超过'b的寿命,并引发错误。现在我可以猜测<'a,'b:'a>表示生命周期'b'必须比生命周期'a'更长。我猜对了吗?还是有更多内容?我该如何找到它的文档记录?