ตอบของตัวเองคำถามที่ซ้ำกัน
Cargo.toml:
[dependencies]
lazy_static = "1.4.0"
รากลัง (lib.rs):
#[macro_use]
extern crate lazy_static;
การเริ่มต้น (ไม่จำเป็นต้องบล็อกที่ไม่ปลอดภัย):
/// EMPTY_ATTACK_TABLE defines an empty attack table, useful for initializing attack tables
pub const EMPTY_ATTACK_TABLE: AttackTable = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
lazy_static! {
/// KNIGHT_ATTACK is the attack table of knight
pub static ref KNIGHT_ATTACK: AttackTable = {
let mut at = EMPTY_ATTACK_TABLE;
for sq in 0..BOARD_AREA{
at[sq] = jump_attack(sq, &KNIGHT_DELTAS, 0);
}
at
};
...
แก้ไข:
จัดการเพื่อแก้ปัญหาด้วย once_cell ซึ่งไม่จำเป็นต้องใช้มาโคร
Cargo.toml:
[dependencies]
once_cell = "1.3.1"
square.rs:
use once_cell::sync::Lazy;
...
/// AttackTable type records an attack bitboard for every square of a chess board
pub type AttackTable = [Bitboard; BOARD_AREA];
/// EMPTY_ATTACK_TABLE defines an empty attack table, useful for initializing attack tables
pub const EMPTY_ATTACK_TABLE: AttackTable = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
/// KNIGHT_ATTACK is the attack table of knight
pub static KNIGHT_ATTACK: Lazy<AttackTable> = Lazy::new(|| {
let mut at = EMPTY_ATTACK_TABLE;
for sq in 0..BOARD_AREA {
at[sq] = jump_attack(sq, &KNIGHT_DELTAS, 0);
}
at
});