gpt4 book ai didi

compiler-errors - 为什么使用 f32::consts::E 会给出错误 E0223 但 std::f32::consts::E 不会?

转载 作者:行者123 更新时间:2023-11-29 08:23:23 24 4
gpt4 key购买 nike

如果我写:

let x = f32::consts::E;

我得到错误:

error[E0223]: ambiguous associated type
--> src/main.rs:32:21
|
32 | let x = f32::consts::E;
| ^^^^^^^^^^^^^^ help: use fully-qualified syntax: `<f32 as Trait>::consts`

但如果我改写:

let x = std::f32::consts::E;

然后一切都很好。错误消息令人困惑,因为据我了解, f32 是特定的具体类型而不是特征。我不确定为什么要使用一些特殊的特征语法。

编译器认为我在做什么,为什么我的修复有帮助?

最佳答案

What does the compiler think I'm doing

有一个名为f32模块 和一个名为f32类型。默认情况下类型随处可用,模块不可用。

没有额外的导入,编译器最好将 f32::foo 理解为类型 f32关联类型,但是有没有这种类型。它假定关联类型来自特征,并建议您更明确地说明它是什么特征。

当您执行 std::f32 时,路径将模块带入作用域,然后可以找到嵌套模块 consts。您还可以:

use std::f32;
let x = f32::consts::E;

这可能发生在任何类型上,但通常类型和模块使用不同的命名风格(UpperCamelCasesnake_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;
}

关于compiler-errors - 为什么使用 f32::consts::E 会给出错误 E0223 但 std::f32::consts::E 不会?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54449836/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com