如何在 Rust 的结构中声明一个“静态”字段,最好使用默认值:
struct MyStruct {
x: i32, // instance
y: i32, // instance
my_static: i32 = 123, // static, how?
}
fn main() {
let a = get_value();
if a == MyStruct::my_static {
//...
} else {
//...
}
}
最佳答案
您可以在 impl 中声明关联常量:
struct MyStruct {
x: i32,
y: i32,
}
impl MyStruct {
const MY_STATIC: i32 = 123;
}
fn main() {
println!("MyStruct::MY_STATIC = {}", MyStruct::MY_STATIC);
}
关于rust - 如何在 Rust 的结构中声明 "static"字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26549480/