3

我想使用float::consts::pi. 例如:

import float::consts::pi;

const pi2:float = pi*pi;

fn main() {
    io::println(#fmt("pi^2=%.4f", pi2));
}

我收到这些错误:

pi2.rs:3:18: 3:20 error: constant contains unimplemented expression type
pi2.rs:3 const pi2:float = pi*pi;
                           ^~
pi2.rs:3:21: 3:23 error: constant contains unimplemented expression type
pi2.rs:3 const pi2:float = pi*pi;
                             ^~

如果我写,编译成功:

const pi2:float = 3.14*3.14;

但如果我定义自己的,则不是pi

const pi:float = 3.141592653589793;
const pi2:float = pi*pi;

我正在使用 rust-0.2。

更新。rust-0.3.1 中的消息更友好,并确认该功能尚未实现。现在允许引用相同的板条箱常量:

$ rustc pi2.rs
pi2.rs:2:18: 2:20 error: paths in constants may only refer to crate-local constants
pi2.rs:2 const pi2:float = pi*pi;
                           ^~
pi2.rs:2:21: 2:23 error: paths in constants may only refer to crate-local constants
pi2.rs:2 const pi2:float = pi*pi;
                              ^~
error: internal compiler error: unexpected failure
note: the compiler hit an unexpected failure path. this is a bug
note: try running with RUST_LOG=rustc=0,::rt::backtrace to get further details and report the results to github.com/mozilla/rust/issues
4

2 回答 2

4

错误消息:

pi2.rs:3:18: 3:20 error: constant contains unimplemented expression type
pi2.rs:3 const pi2:float = pi*pi;
                           ^~
pi2.rs:3:21: 3:23 error: constant contains unimplemented expression type
pi2.rs:3 const pi2:float = pi*pi;
                             ^~

意味着目前在 Rust 0.2 编译器中没有针对这些情况的实现。Rust 无法解析标识符的值pi

所需的功能可能会出现在 Rust 的未来版本中。

于 2012-04-01T11:47:22.160 回答
0

我无法验证,因为目前我无法在我的机器上编译 Rust,但我认为你的问题可能是pi常量被定义为 typec_float而不是 type float。尝试:

const pi2: c_float = pi*pi;

看看有没有效果。如果还需要导入类型,您可能还需要一个导入行import libc::c_float;

于 2012-04-01T03:25:30.863 回答