Trả lời câu hỏi trùng lặp của riêng tôi .
Cargo.toml:
[dependencies]
lazy_static = "1.4.0"
Gốc thùng (lib.rs):
#[macro_use]
extern crate lazy_static;
Khởi tạo (không cần khối không an toàn):
/// 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
};
...
BIÊN TẬP:
Được quản lý để giải quyết nó bằng once_cell, không cần macro.
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
});