【发布时间】:2023-01-22 17:55:04
【问题描述】:
为什么这段代码不起作用?
pub struct Foo {}
impl Foo {
const THREE: i32 = 3;
pub fn mul_three(num: i32) -> i32 {
num * THREE
}
pub fn sub_three(num: i32) -> i32 {
num - THREE
}
}
如果将常量向上移动到模块级别或向下移动到函数中,它就会起作用。但是,尽管它在当前的位置在语法上是允许的,但它不可用:
error[E0425]: cannot find value `THREE` in this scope
--> <source>:6:15
|
6 | num * THREE
| ^^^^^ not found in this scope
这背后的技术原因是什么?
【问题讨论】:
标签: rust