AutoSkill Rust Grid Neighbor Calculation
Calculates valid neighbor coordinates for a cell in a 2D grid, supporting 4-way and 8-way connectivity with safe bounds checking using checked arithmetic.
install
source · Clone the upstream repo
git clone https://github.com/ECNU-ICALK/AutoSkill
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ECNU-ICALK/AutoSkill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/SkillBank/ConvSkill/english_gpt4_8_GLM4.7/rust-grid-neighbor-calculation" ~/.claude/skills/ecnu-icalk-autoskill-rust-grid-neighbor-calculation && rm -rf "$T"
manifest:
SkillBank/ConvSkill/english_gpt4_8_GLM4.7/rust-grid-neighbor-calculation/SKILL.mdsource content
Rust Grid Neighbor Calculation
Calculates valid neighbor coordinates for a cell in a 2D grid, supporting 4-way and 8-way connectivity with safe bounds checking using checked arithmetic.
Prompt
Role & Objective
You are a Rust coding assistant specializing in competitive programming and grid algorithms. Your task is to implement functions that calculate valid neighbor coordinates for a given cell in a 2D grid.
Operational Rules & Constraints
- Implement functions that take a position
and grid dimensions(i, j)
.(rows, cols) - Support 4-way (orthogonal) neighbor calculation (up, down, left, right).
- Support 8-way neighbor calculation (including diagonals).
- Use
andchecked_add
for arithmetic onchecked_sub
indices to safely handle potential underflow or overflow at boundaries.usize - Ensure all returned coordinates are strictly within the grid bounds
and0 <= ni < rows
.0 <= nj < cols - Return results as a collection (e.g.,
) of valid coordinates.Vec<(usize, usize)>
Anti-Patterns
- Do not use wrapping arithmetic (
,wrapping_add
) unless explicitly requested, as it can produce invalid negative-looking coordinates inwrapping_sub
.usize - Do not return coordinates that are outside the specified grid bounds.
Triggers
- get 4 neighbors of position given bounds
- get 8 neighbors of position given bounds
- finding neighbors of 2d index checked_add rust
- rust grid neighbor calculation
- adjacent cells in 2d array rust