编译器认为我在做什么
有一个
打电话
f32
还有一个
打电话
f32
在不增加进口的情况下,,
f32::foo
编译器最好将其理解为
关联类型
对于类型
f32
std::f32
consts
然后可以找到。您还可以执行以下操作:
use std::f32;
let x = f32::consts::E;
UpperCamelCase
snake_case
):
struct my_type;
mod other {
pub mod my_type {
pub mod consts {
pub const ZERO: i32 = 0;
}
}
}
fn example() {
my_type::consts::ZERO;
}
error[E0223]: ambiguous associated type
--> src/lib.rs:12:5
|
12 | my_type::consts::ZERO;
| ^^^^^^^^^^^^^^^^^^^^^ help: use fully-qualified syntax: `<my_type as Trait>::consts`
原语恰好使用所有小写字母。
下面是一些代码(用途可疑),它显示了关联类型实际上是如何发生的:
struct Consts;
impl Consts {
const E: char = 'e';
}
trait Example {
type consts;
}
impl Example for f32 {
type consts = Consts;
}
fn example() {
<f32 as Example>::consts::E;
}