为什么Rust在给出Self::from(x)
的警告时,不给出使用x.into()
方法的"无条件递归"警告?
考虑下面的示例,in rust playground:
struct Foo;
struct Bar;
impl From<Foo> for Bar {
fn from(x: Foo) -> Self {
x.into() // No recursion warning
}
}
fn main() {}
考虑显示递归警告in rust playground的另一个示例
struct Foo;
struct Bar;
impl From<Foo> for Bar {
fn from(x: Foo) -> Self {
Self::from(x) // Shows recursion warning
}
}
fn main() {}
warning: function cannot return without recursing
--> src/main.rs:5:5
|
5 | fn from(x: Foo) -> Self {
| ^^^^^^^^^^^^^^^^^^^^^^^ cannot return without recursing
6 | Self::from(x)
| ------------- recursive call site
|
= note: `#[warn(unconditional_recursion)]` on by default
= help: a `loop` may express intention better if this is on purpose