#![warn(single_use_lifetimes)]
fn do_foo() {
#[derive(Debug)]
struct Foo<'a> {
bar: &'a u32,
}
}
Run Code Online (Sandbox Code Playgroud)
导致此警告:
warning: lifetime parameter `'a` only used once
--> src/lib.rs:6:16
|
6 | struct Foo<'a> {
| ^^
|
Run Code Online (Sandbox Code Playgroud)
这个警告意味着什么?怎么解决这个问题?
省略派生或功能时不显示此警告.
目的是防止像这样的代码,其中生命周期没有明确指定:
pub fn example<'a>(_val: SomeType<'a>) {}
Run Code Online (Sandbox Code Playgroud)
相反,它最好使用'_:
pub fn example(_val: SomeType<'_>) {}
Run Code Online (Sandbox Code Playgroud)
如果你扩展你的代码并将其修剪下来,你得到这个:
use std::fmt;
struct Foo<'a> {
bar: &'a u32,
}
impl<'a> fmt::Debug for Foo<'a> {
fn fmt(&self, _f: &mut fmt::Formatter) -> fmt::Result { Ok(()) }
}
Run Code Online (Sandbox Code Playgroud)
warning: lifetime parameter `'a` only used once
--> src/lib.rs:9:6
|
9 | impl<'a> fmt::Debug for Foo<'a> {
| ^^
|
Run Code Online (Sandbox Code Playgroud)
也就是说,<'a>不需要,但是无论如何派生都会添加它(因为自动生成代码很难).
老实说,我不知道代码会在这里改变什么,因为你不能使用'_struct的通用生命周期......
怎么解决这个问题?
我不知道它可以,不重写derive执行Debug.
也可以看看:
| 归档时间: |
|
| 查看次数: |
91 次 |
| 最近记录: |