Skip to content

morganlinton/BeautifulRust / skills/rust-best-practices

rust-best-practices

Unverified

Write idiomatic, robust, maintainable Rust. Use this skill whenever you write, review, refactor, debug, or explain Rust code: new crates, modules, functions, structs, traits, error types, async code, unsafe or FFI code, Cargo.toml, tests, or rustdoc. Trigger on any mention of Rust, cargo, crates, .rs files, the borrow checker, lifetimes, traits, clippy, or rustfmt, even when the user does not ask for "best practices" or "idiomatic" code. Synthesized from the Google Rust Style Guide, the Rust API Guidelines, Effective Rust, Rust Design Patterns, and Microsoft's Pragmatic Rust Guidelines.

FileKindSize
SKILL.mdskill md29.0 KB
evals/bevy-arena-regen/case.yamlconfig783 B
evals/bevy-arena-regen/files/Cargo.lockmanifest32.5 KB
evals/bevy-arena-regen/files/Cargo.tomlmanifest354 B
evals/bevy-arena-regen/files/src/lib.rsscript6.2 KB
evals/bevy-arena-regen/files/tests/arena.rsscript3.3 KB
evals/bevy-arena-regen/graders/01-builds-and-tests-pass.mdmarkdown167 B
evals/bevy-arena-regen/graders/02-regen-tested-headless.mdmarkdown272 B
evals/bevy-arena-regen/graders/03-system-params-by-value.mdmarkdown212 B
evals/bevy-arena-regen/graders/04-keeps-bevy-prelude.mdmarkdown159 B
evals/bevy-arena-regen/graders/05-plain-data-components.mdmarkdown208 B
evals/bevy-arena-regen/graders/06-ordered-after-damage.mdmarkdown334 B
evals/bevy-arena-regen/graders/07-no-panicking-queries.mdmarkdown186 B
evals/bevy-arena-regen/graders/08-clippy-clean.mdmarkdown125 B
evals/bevy-arena-regen/graders/09-health-never-exceeds-max.mdmarkdown134 B
evals/bevy-arena-regen/graders/10-existing-code-not-rewritten.mdmarkdown275 B
evals/bevy-arena-regen/place-files.shexecutablescript62 B
evals/bevy-arena-regen/prompt.mdmarkdown480 B
evals/log-summary-cli/case.yamlconfig581 B
evals/log-summary-cli/graders/01-never-panics-on-bad-input.mdmarkdown212 B
evals/log-summary-cli/graders/02-errors-name-the-file.mdmarkdown104 B
evals/log-summary-cli/graders/03-malformed-lines-counted.mdmarkdown186 B
evals/log-summary-cli/graders/04-paths-as-path-types.mdmarkdown168 B
evals/log-summary-cli/graders/05-sorted-and-skipped-count.mdmarkdown150 B
evals/log-summary-cli/graders/06-dependency-ranges.mdmarkdown134 B
evals/log-summary-cli/graders/07-map-based-counting.mdmarkdown103 B
evals/log-summary-cli/graders/08-aggregation-unit-tested.mdmarkdown147 B
evals/log-summary-cli/prompt.mdmarkdown730 B
evals/refactor-inventory-module/case.yamlconfig677 B
evals/refactor-inventory-module/files/inventory.rsscript3.2 KB
evals/refactor-inventory-module/graders/01-deny-warnings-removed.mdmarkdown90 B
evals/refactor-inventory-module/graders/02-no-rc-refcell-in-api.mdmarkdown141 B
evals/refactor-inventory-module/graders/03-borrowed-param-types.mdmarkdown157 B
evals/refactor-inventory-module/graders/04-error-implements-error.mdmarkdown108 B
evals/refactor-inventory-module/graders/05-no-unwrap-on-fallible-paths.mdmarkdown209 B
evals/refactor-inventory-module/graders/06-getter-renamed.mdmarkdown132 B
evals/refactor-inventory-module/graders/07-bool-param-replaced.mdmarkdown117 B
evals/refactor-inventory-module/graders/08-deterministic-report.mdmarkdown114 B
evals/refactor-inventory-module/graders/09-format-over-concat.mdmarkdown119 B
evals/refactor-inventory-module/graders/10-no-check-then-unwrap.mdmarkdown128 B
evals/refactor-inventory-module/graders/11-changes-listed-with-reasons.mdmarkdown94 B
evals/refactor-inventory-module/graders/12-signature-changes-called-out.mdmarkdown201 B
evals/refactor-inventory-module/place-files.shexecutablescript62 B
evals/refactor-inventory-module/prompt.mdmarkdown507 B
evals/semver-library-api/case.yamlconfig575 B
evals/semver-library-api/graders/01-implements-fromstr.mdmarkdown141 B
evals/semver-library-api/graders/02-implements-display.mdmarkdown94 B
evals/semver-library-api/graders/03-dedicated-error-type.mdmarkdown164 B
evals/semver-library-api/graders/04-common-traits-derived.mdmarkdown127 B
evals/semver-library-api/graders/05-fields-private.mdmarkdown125 B
evals/semver-library-api/graders/06-rustdoc-with-errors-section.mdmarkdown132 B
evals/semver-library-api/graders/07-doc-examples-use-question-mark.mdmarkdown86 B
evals/semver-library-api/graders/08-precedence-rules-tested.mdmarkdown152 B
evals/semver-library-api/graders/09-no-unwrap-in-library-code.mdmarkdown136 B
evals/semver-library-api/graders/10-eq-hash-agree-with-ord.mdmarkdown258 B
evals/semver-library-api/prompt.mdmarkdown667 B
references/bevy.mdmarkdown7.0 KB
references/effective-rust.mdmarkdown13.9 KB
references/google-rust-style-guide.mdmarkdown10.7 KB
references/microsoft-rust-guidelines.mdmarkdown16.2 KB
references/rust-api-guidelines.mdmarkdown11.2 KB
references/rust-design-patterns.mdmarkdown8.2 KB

evals/bevy-arena-regen/files/src/lib.rs

View on GitHub
//! A wave-based arena: the player fights waves of enemies that walk toward them.

use bevy::prelude::*;

/// Hit points. An entity at zero is dead and gets despawned.
#[derive(Component, Debug, Clone, Copy, PartialEq, Eq)]
pub struct Health(pub u32);

/// Flat damage reduction applied to every hit.
#[derive(Component, Debug, Clone, Copy, PartialEq, Eq)]
pub struct Armor(pub u32);

/// World position in pixels.
#[derive(Component, Debug, Clone, Copy, PartialEq)]
pub struct Position(pub Vec2);

/// Movement in pixels per second.
#[derive(Component, Debug, Clone, Copy, PartialEq)]
pub struct Velocity(pub Vec2);

/// Marks the player.
#[derive(Component, Debug)]
pub struct Player;

/// Marks an enemy.
#[derive(Component, Debug)]
pub struct Enemy;

/// A stunned unit neither moves nor deals contact damage.
#[derive(Component, Debug)]
pub struct Stunned;

/// Damage queued against an entity this frame.
#[derive(Message, Debug, Clone, Copy)]
pub struct Hit {
    pub target: Entity,
    pub amount: u32,
    pub kind: HitKind,
}

/// Whether a hit is a normal or a critical one.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HitKind {
    Normal,
    /// Deals half again as much damage.
    Critical,
}

/// Wave progress.
#[derive(Resource, Debug, Default)]
pub struct Wave {
    pub number: u32,
    pub cleared: bool,
}

/// Points for kills.
#[derive(Resource, Debug, Default)]
pub struct Score(pub u32);

/// Tuning values for the arena.
#[derive(Resource, Debug, Clone)]
pub struct ArenaConfig {
    pub enemies_per_wave: u32,
    pub enemy_health: u32,
    pub contact_damage: u32,
    pub contact_range: f32,
    pub spawn_radius: f32,
    pub enemy_speed: f32,
}

impl Default for ArenaConfig {
    fn default() -> Self {
        Self {
            enemies_per_wave: 6,
            enemy_health: 30,
            contact_damage: 5,
            contact_range: 16.0,
            spawn_radius: 200.0,
            enemy_speed: 40.0,
        }
    }
}

/// Adds the arena's resources and systems.
pub struct ArenaPlugin;

impl Plugin for ArenaPlugin {
    fn build(&self, app: &mut App) {
        app.init_resource::<Wave>()
            .init_resource::<Score>()
            .init_resource::<ArenaConfig>()
            .add_message::<Hit>()
            .add_systems(Startup, spawn_player)
            .add_systems(Update, (move_units, contact_damage, apply_hits, stun_wounded).chain())
            .add_systems(Update, (check_wave_cleared, spawn_wave).chain());
    }
}

/// Spawns the player at the center of the arena.
pub fn spawn_player(mut commands: Commands) {
    commands.spawn((
        Player,
        Health(100),
        Armor(2),
        Position(Vec2::ZERO),
        Velocity(Vec2::ZERO),
    ));
}

/// Moves every unit that isn't stunned.
pub fn move_units(time: Res<Time>, mut units: Query<(&mut Position, &Velocity), Without<Stunned>>) {
    let dt = time.delta_secs();
    for (mut position, velocity) in &mut units {
        position.0 += velocity.0 * dt;
    }
}

/// Enemies touching the player hit them. Does nothing once the player is gone.
pub fn contact_damage(
    config: Res<ArenaConfig>,
    player: Single<(Entity, &Position), With<Player>>,
    enemies: Query<&Position, (With<Enemy>, Without<Stunned>)>,
    mut hits: MessageWriter<Hit>,
) {
    let (player, player_position) = *player;
    for position in &enemies {
        if position.0.distance(player_position.0) <= config.contact_range {
            hits.write(Hit {
                target: player,
                amount: config.contact_damage,
                kind: HitKind::Normal,
            });
        }
    }
}

/// Applies this frame's hits, despawns whatever dies, and scores enemy kills.
pub fn apply_hits(
    mut commands: Commands,
    mut hits: MessageReader<Hit>,
    mut players: Query<(&mut Health, Option<&Armor>), (With<Player>, Without<Enemy>)>,
    mut enemies: Query<(&mut Health, Option<&Armor>), (With<Enemy>, Without<Player>)>,
    mut score: ResMut<Score>,
) {
    for hit in hits.read() {
        let (mut health, armor) = if let Ok(target) = players.get_mut(hit.target) {
            target
        } else if let Ok(target) = enemies.get_mut(hit.target) {
            target
        } else {
            continue;
        };
        let was_alive = health.0 > 0;
        let dealt = damage_after_armor(hit.amount, armor.map_or(0, |a| a.0), hit.kind);
        health.0 = health.0.saturating_sub(dealt);
        if was_alive && health.0 == 0 {
            if enemies.contains(hit.target) {
                score.0 += 10;
            }
            commands.entity(hit.target).despawn();
        }
    }
}

/// Enemies hurt below a quarter of their health, plus their armor, are stunned for the
/// rest of the wave.
pub fn stun_wounded(
    mut commands: Commands,
    config: Res<ArenaConfig>,
    wounded: Query<(Entity, &Health, Option<&Armor>), (With<Enemy>, Without<Stunned>, Changed<Health>)>,
) {
    for (entity, health, armor) in &wounded {
        let threshold = config.enemy_health / 4 + armor.map_or(0, |a| a.0);
        if health.0 > 0 && health.0 < threshold {
            commands.entity(entity).insert(Stunned);
        }
    }
}

/// Damage left after armor; critical hits deal half again as much, rounded down.
pub fn damage_after_armor(raw: u32, armor: u32, kind: HitKind) -> u32 {
    let base = raw.saturating_sub(armor);
    match kind {
        HitKind::Normal => base,
        HitKind::Critical => base.saturating_add(base / 2),
    }
}

/// Spawns the next wave in a ring around the player once the last one is cleared.
pub fn spawn_wave(mut commands: Commands, config: Res<ArenaConfig>, mut wave: ResMut<Wave>) {
    if !wave.cleared {
        return;
    }
    wave.cleared = false;
    for i in 0..config.enemies_per_wave {
        let direction = Vec2::from_angle(i as f32 / config.enemies_per_wave as f32 * std::f32::consts::TAU);
        commands.spawn((
            Enemy,
            Health(config.enemy_health),
            Position(direction * config.spawn_radius),
            Velocity(-direction * config.enemy_speed),
        ));
    }
}

/// Starts the next wave once no enemies are left.
pub fn check_wave_cleared(enemies: Query<(), With<Enemy>>, mut wave: ResMut<Wave>) {
    if !wave.cleared && enemies.is_empty() {
        wave.number += 1;
        wave.cleared = true;
    }
}
finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the workStars: 322kUpdated Claude CodeCodexVerifiedInstall
systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixesStars: 322kUpdated Claude CodeCodexVerifiedInstall
test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation codeStars: 322kUpdated Claude CodeCodexVerifiedInstall
skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy.Stars: 198kUpdated Claude CodeCodexVerifiedInstall