为数字类型创建类型别名时关联类型不明确
Ambiguous associated type when making a type alias for a numeric type
我正在编写一个几何库并正在处理表示长度的值。我已经为表示长度的类型编写了一个类型别名,目前设置为 f64
但将来可能会更改为 f32
或其他可能的数字类型(可能取决于精度和 space 要求):
type Length = f64;
这个库中的所有函数和结构都将按照这种类型编写,例如这个函数:
fn circumference(radius: Length) -> Length {
Length::consts::PI * radius * radius
}
但是,上述函数会导致以下编译器错误:
Compiling playground v0.0.1 (/playground)
error[E0223]: ambiguous associated type
--> src/lib.rs:4:5
|
4 | Length::consts::PI * radius * radius
| ^^^^^^^^^^^^^^ help: use fully-qualified syntax: `<f64 as Trait>::consts`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0223`.
error: could not compile `playground`
To learn more, run the command again with --verbose.
but doesn't give any elaboration about the Trait I should use — presumably I need to write <Length as Trait>::consts::PI
for some appropriately chosen Trait
, but it doesn't tell me what this trait should是。
应该是什么特质?
您无法访问 std::f64::consts::PI
through the type alias Length
. That PI
is a module constant belonging to the consts
submodule of std::f64
. It isn't associated with the f64
原始类型。
要访问模块常量,您需要使用其完全限定名称 (::std::f64::consts::PI
) 或在其路径中导入其中一个模块并使用部分限定名称。例如,
use std::f64;
fn circumference(radius: Length) -> Length {
f64::consts::PI * radius * radius
}
// or
use std::f64::consts as foo;
fn circumference(radius: Length) -> Length {
foo::PI * radius * radius
}
为避免混淆 Length
与其当前定义,您始终可以定义一个局部常量,如
const LENGTH_PI: Length = f64::consts::PI as Length;
这样,以后Length
的类型发生变化时,只需替换f64::consts::PI
一次即可。
我正在编写一个几何库并正在处理表示长度的值。我已经为表示长度的类型编写了一个类型别名,目前设置为 f64
但将来可能会更改为 f32
或其他可能的数字类型(可能取决于精度和 space 要求):
type Length = f64;
这个库中的所有函数和结构都将按照这种类型编写,例如这个函数:
fn circumference(radius: Length) -> Length {
Length::consts::PI * radius * radius
}
但是,上述函数会导致以下编译器错误:
Compiling playground v0.0.1 (/playground)
error[E0223]: ambiguous associated type
--> src/lib.rs:4:5
|
4 | Length::consts::PI * radius * radius
| ^^^^^^^^^^^^^^ help: use fully-qualified syntax: `<f64 as Trait>::consts`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0223`.
error: could not compile `playground`
To learn more, run the command again with --verbose.
but doesn't give any elaboration about the Trait I should use — presumably I need to write <Length as Trait>::consts::PI
for some appropriately chosen Trait
, but it doesn't tell me what this trait should是。
应该是什么特质?
您无法访问 std::f64::consts::PI
through the type alias Length
. That PI
is a module constant belonging to the consts
submodule of std::f64
. It isn't associated with the f64
原始类型。
要访问模块常量,您需要使用其完全限定名称 (::std::f64::consts::PI
) 或在其路径中导入其中一个模块并使用部分限定名称。例如,
use std::f64;
fn circumference(radius: Length) -> Length {
f64::consts::PI * radius * radius
}
// or
use std::f64::consts as foo;
fn circumference(radius: Length) -> Length {
foo::PI * radius * radius
}
为避免混淆 Length
与其当前定义,您始终可以定义一个局部常量,如
const LENGTH_PI: Length = f64::consts::PI as Length;
这样,以后Length
的类型发生变化时,只需替换f64::consts::PI
一次即可。