MCPInstall

rust-by-example

Show Rust patterns through short compilable examples — type conversions (From/Into/TryFrom/as/Deref), flow control (if let/while let/match/loop), functions and closures (Fn/FnMut/FnOnce, captures), modules (mod/use/pub/super/self), generics and traits (bounds/associated types/trait objects), error handling (?/Result/thiserror/anyhow), attributes (derive/cfg/inline/allow), unsafe (raw pointers/unions/ABI), procedural macros (derive/attribute/function-like), and inline asm. Use when users ask "how do I write X in Rust", need a concrete pattern with copy-pasteable code, or are migrating from Java/Python/Go/C++ and want the Rust equivalent; hand architecture decisions to rust-api-design/rust-workspace, std API selection to rust-stdlib, and async runtime to rust-concurrency.

Installs
97
Last 24h
—
Trending rank
—
Security
Not audited

Install

Works with every agent the skills CLI supports.

npx skills add https://github.com/full-stack-skills/rust-skills --skill rust-by-example
  1. 01

    Install

    Run the command in your project. The CLI asks which agents to install for.

  2. 02

    Describe the task

    Ask your agent for something this skill covers. It loads rust-by-example automatically when relevant.

  3. 03

    Review the result

    The agent follows the SKILL.md steps, scripts and references bundled with the skill.

SKILL.md

The instructions your agent reads when the skill is triggered.

Rust by Example — Patterns Through Compilable Snippets

Authority: Rust by Example — the official example-driven tutorial. This skill condenses its 24 chapters into a routing layer that points to topic-specific reference files.

This skill owns "how do I write X" patterns with concrete code. It does not own architectural decisions (rust-api-design, rust-workspace), language semantics (rust-stable), or std API selection (rust-stdlib).

Capability Boundaries

✅ Strengths

  1. Converting between types — From/Into/TryFrom/as/Deref/transmute
  2. Flow control idioms — if let, while let, match, loop with labels, ?
  3. Functions, methods, closures (Fn/FnMut/FnOnce), capture modes
  4. Modules, use, pub, super/self/crate, file-based module resolution
  5. Generics — functions, structs, methods, impl blocks, where clauses
  6. Traits — definition, default methods, associated types, trait objects, object safety
  7. Error handling — ?, Result, Option, custom errors with thiserror, anyhow
  8. Attributes — #[derive], #[cfg], #[inline], #[allow], #[non_exhaustive]
  9. Unsafe — raw pointers, dereferences, unsafe fn/unsafe impl, unions, ABI
  10. Procedural macros — derive, attribute, function-like (high-level routing to rust-macros)
  11. Inline assembly — core::arch::asm! (rare but supported)

⚠️ Prerequisites

  1. Basic Rust syntax — see rust-stable
  2. Project structure — see rust-workspace, rust-module-layout

❌ Out of Scope

  1. API design decisions → rust-api-design
  2. Workspace / module layout → rust-workspace, rust-module-layout
  3. Std API selection (which collection, which pointer) → rust-stdlib
  4. Async runtime → rust-concurrency
  5. Deep unsafe / FFI → rust-unsafe-ffi
  6. Authoring complex procedural macros → rust-macros

Data Privacy

This skill does not collect, store, or transmit user data.


Topic Map (One Section per Pattern Category)

Each section has a short example inline; deeper patterns live in the linked reference file.

1. Type Conversions

// From / Into (lossless)
impl From<i32> for Wrapped { fn from(v: i32) -> Self { Self(v) } }
let w: Wrapped = 42.into();   // Into comes for free

// TryFrom (fallible)
impl TryFrom<&str> for Email {
    type Error = EmailError;
    fn try_from(s: &str) -> Result<Self, Self::Error> { /* */ }
}

// as (numeric widening/narrowing — can lose data)
let big: i64 = 1_000_000;
let small: i32 = big as i32;       // ❌ silent truncation possible

// Deref (smart-pointer coercion, NOT polymorphism)
let s: String = "hi".to_string();
let s_ref: &str = &s;              // Deref<Target=str> kicks in

See references/conversions.md for as vs From vs TryFrom, when each is correct.

2. Flow Control

// if let — single-pattern destructure
if let Some(x) = opt { /* */ }

// while let — loop until None
while let Some(line) = reader.lines().next() { /* */ }

// match with @-binding
match point {
    Point { x: 0, y } => println!("on y-axis at {y}"),
    Point { x, y: 0 } => println!("on x-axis at {x}"),
    p @ Point { x: 10..=20, .. } => println!("in x-band: {p:?}"),
    _ => {}
}

// loop with label
'outer: loop {
    loop { break 'outer; }
}

See references/flow-control.md for match ergonomics, guards, ranges, and when to prefer if let.

3. Functions and Closures

// Closure captures by ref/move based on use
let v = vec![1, 2, 3];
let print = || println!("{:?}", v);       // borrows
let consume = move || { v.into_iter().sum() };  // moves

// Fn / FnMut / FnOnce — how a closure borrows its environment
fn apply(f: impl Fn()) { f() }            // can be called multiple times, immutable capture
fn apply_mut(mut f: impl FnMut()) { f() } // mutable capture
fn apply_once(f: impl FnOnce() -> i32) -> i32 { f() }  // consumes captured

See references/closures.md for move keyword, return-type inference, and Fn/FnMut/FnOnce trait bounds.

4. Modules

// src/lib.rs
pub mod network;                  // src/network.rs OR src/network/mod.rs
pub use network::Client;          // re-export

// src/network.rs
pub struct Client { /* */ }
pub fn connect() { /* */ }
// Using items
use std::collections::HashMap;
use std::io::{self, Read};        // io module + Read trait
use crate::network::Client;       // absolute
use super::helper;                 // parent
use self::inner::Item;            // current

See references/modules.md for pub/pub(crate)/pub(super), file-based vs mod.rs, and crate root re-exports. For deep layout work, route to rust-module-layout.

5. Generics

// Generic function with bound
fn first<T>(xs: &[T]) -> Option<&T> { xs.first() }

// Generic with where clause
fn sum<T>(xs: &[T]) -> T where T: Default + std::ops::AddAssign + Copy {
    let mut acc = T::default();
    for x in xs { acc += *x; }
    acc
}

// Generic struct
struct Pair<A, B> { a: A, b: B }

impl<A: Clone, B: Clone> Pair<A, B> {
    fn swapped(&self) -> Pair<B, A> { Pair { a: self.b.clone(), b: self.a.clone() } }
}

See references/generics-traits.md for monomorphization cost, when to use generics vs trait objects, and associated types vs generics.

6. Traits

// Definition with default method
pub trait Summary {
    fn summarize(&self) -> String;
    fn default_summary(&self) -> String { format!("(no summary)") }
}

// Trait object (dynamic dispatch)
fn print_summary(item: &dyn Summary) { println!("{}", item.summarize()); }

// Generic (static dispatch)
fn print_summary_generic<T: Summary>(item: &T) { println!("{}", item.summarize()); }

// Associated type
trait Container { type Item; fn first(&self) -> Option<&Self::Item>; }

Object safety rules: no Self in method signatures, no generic methods, sized receivers. See references/generics-traits.md.

7. Error Handling

use std::fs;
use std::io;
use std::num::ParseIntError;

// ? propagates with From conversion
fn read_count() -> Result<i32, io::Error> {
    let s: String = fs::read_to_string("count.txt")?;
    Ok(s.trim().parse().unwrap_or(0))   // or proper error mapping
}

// Custom error with thiserror
#[derive(thiserror::Error, Debug)]
enum Error {
    #[error("io: {0}")] Io(#[from] io::Error),
    #[error("parse: {0}")] Parse(#[from] ParseIntError),
    #[error("not found: {key}")] NotFound { key: String },
}

// Application-layer catch-all with anyhow
fn main() -> anyhow::Result<()> {
    let x: i32 = "5".parse()?;
    Ok(())
}

See references/error-handling.md for ? with From, thiserror vs anyhow, and custom error design.

8. Attributes

#[derive(Debug, Clone, PartialEq)]   // generate traits
struct User { id: u32 }

#[cfg(target_os = "linux")]          // conditional compilation
fn setup() { /* */ }

#[inline]                            // hint to inline (rarely matters)
fn fast() {}

#[allow(dead_code)]                  // suppress warning
struct Unused;

#[non_exhaustive]                    // allow adding variants
enum Event { Login }

See references/attributes.md for the full attribute catalog, cfg expressions, and when #[inline] actually helps.

9. Unsafe

// Raw pointer dereference
let x = 42;
let ptr: *const i32 = &x;
let val = unsafe { *ptr };

// Splitting a slice (needs unsafe for non-overlapping aliasing)
let mut v = vec![1, 2, 3, 4];
let (left, right) = v.split_at_mut(2);   // safe wrapper

// Calling C ABI
extern "C" { fn abs(input: i32) -> i32; }
let a = unsafe { abs(-5) };

// Union (unsafe to read)
#[repr(C)]
union Value { i: i32, f: f32 }

See references/unsafe.md for safety contracts, the four unsafe superpowers, and when to reach for it. For deep FFI, route to rust-unsafe-ffi.

10. Procedural Macros (overview only)

// Derive (most common)
#[derive(Debug, Clone, serde::Serialize)]
struct Config { /* */ }

// Attribute macro
#[tokio::main]
async fn main() { /* */ }

// Function-like macro
let html = html! { <div>Hello</div> };

Authoring these is complex — route to rust-macros. This skill only covers using them.

See references/procedural-macros-overview.md for the three flavors and when each applies.

11. Inline Assembly

#[cfg(target_arch = "x86_64")]
fn halt() {
    unsafe { std::arch::asm!("hlt", options(nostack)); }
}

See references/inline-asm.md for asm! syntax, clobbers, and when to use it (kernel/embedded only).


Workflow

  1. Identify the question — "how do I write X" → this skill; "which type" → rust-stdlib; "how to architect" → rust-api-design.
  2. Find the matching reference — Sections 1-11 above, or the file in references/.
  3. Read the minimal example — copy the pattern, adapt to your code.
  4. Verify — cargo check, cargo clippy.
  5. Hand off — deeper questions route to specialized skills.

Decision Shortcuts

QuestionWhere
How do I convert types?references/conversions.md
How do I write if let/match?references/flow-control.md
How do closures capture?references/closures.md
How do mod/use/pub work?references/modules.md
Generic fn/struct/impl?references/generics-traits.md
Trait object vs generic?references/generics-traits.md
How do I propagate errors?references/error-handling.md
What #[derive]/#[cfg] are there?references/attributes.md
When is unsafe OK?references/unsafe.md
How do I write a procedural macro?rust-macros skill
What does asm! look like?references/inline-asm.md

Coming From Another Language

Pattern in......becomes in Rust
Java interface + implRust trait + impl block
Java generics <T extends Foo>Rust <T: Foo> or where T: Foo
Java Optional<T>Rust Option<T>
Java try/catchRust ? + Result
Python withRust Drop (RAII)
Python decoratorsRust attribute macros
Go goroutineRust thread::spawn or async task
Go interface{}Rust dyn Trait or generics
C++ RAII destructorRust Drop trait
C++ reinterpret_castRust unsafe { transmute } (avoid)

See references/migrating-from-other-languages.md for a fuller mapping.

Resources

  • Type Conversions
  • Flow Control
  • Closures
  • Modules
  • Generics and Traits
  • Error Handling
  • Attributes
  • Unsafe
  • Procedural Macros Overview
  • Inline Assembly
  • Migrating From Other Languages
  • examples/golden-by-example/: a crate exercising each pattern

Upstream Sources

FAQ

What does the rust-by-example skill do?

Show Rust patterns through short compilable examples — type conversions (From/Into/TryFrom/as/Deref), flow control (if let/while let/match/loop), functions and closures (Fn/FnMut/FnOnce, captures), modules (mod/use/pub/super/self), generics and traits (bounds/associated types/trait objects), error handling (?/Result/thiserror/anyhow), attributes (derive/cfg/inline/allow), unsafe (raw pointers/unions/ABI), procedural macros (derive/attribute/function-like), and inline asm. Use when users ask "how do I write X in Rust", need a concrete pattern with copy-pasteable code, or are migrating from Java/Python/Go/C++ and want the Rust equivalent; hand architecture decisions to rust-api-design/rust-workspace, std API selection to rust-stdlib, and async runtime to rust-concurrency.

How do I install rust-by-example?

Run npx skills add https://github.com/full-stack-skills/rust-skills --skill rust-by-example in your project. The skills CLI detects the agents you use and copies the skill into the right folder. For Claude Code you can also place the skill folder at ~/.claude/skills/rust-by-example/ (personal) or .claude/skills/rust-by-example/ (project).

Which AI agents can use rust-by-example?

Any agent that supports the open Agent Skills format (a folder with a SKILL.md file), including Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, Windsurf, Cline, OpenCode and more.

Is rust-by-example safe to install?

No partner security audit is available yet. Skills can run commands and scripts, so read the SKILL.md and any bundled files before installing.

Who maintains rust-by-example?

It is published by full-stack-skills in full-stack-skills/rust-skills. It has 97 recorded installs.

More from full-stack-skills/rust-skills

View all →

rust-testing

full-stack-skills/rust-skills

Design, implement, and validate Rust tests, including unit, integration, doctest, compile-fail, property, fuzz, benchmark, coverage, async, concurrency, process, daemon, IPC, terminal, platform, and hardware-facing test strategies. Use when users ask for Rust test architecture, flaky-test diagnosis, coverage gates, benchmarks, trybuild, cargo-nextest, real-process tests, or failure-path verification.

Testing & QA148 installs

rust-concurrency

full-stack-skills/rust-skills

Open the skill to read its SKILL.md instructions.

Backend & APIs137 installs

rust-style-clippy

full-stack-skills/rust-skills

Open the skill to read its SKILL.md instructions.

Backend & APIs137 installs

rust-performance

full-stack-skills/rust-skills

Open the skill to read its SKILL.md instructions.

Backend & APIs135 installs

rust-code-review

full-stack-skills/rust-skills

Open the skill to read its SKILL.md instructions.

rust-cargo-build

full-stack-skills/rust-skills

Open the skill to read its SKILL.md instructions.

Backend & APIs123 installs

Popular backend & apis skills

View category →

find-skills

vercel-labs/skills

Helps users discover and install agent skills when they ask questions like "how do I do X", "find a skill for X", "is there a skill that can...", or express interest in extending capabilities. This skill should be used when the user is looking for functionality that might exist as an installable skill.

OfficialBackend & APIs4.1M installs

entra-app-registration

microsoft/azure-skills

Guides Microsoft Entra ID app registration, OAuth 2.0 authentication, and MSAL integration. USE FOR: create app registration, register Azure AD app, configure OAuth, set up authentication, add API permissions, generate service principal, MSAL example, console app auth, Entra ID setup, Azure AD authentication. DO NOT USE FOR: Key Vault secrets (use azure-keyvault-expiration-audit), general Azure resource security guidance.

OfficialBackend & APIs566.7K installs

This skill should be used when the user wants to "write agent code", "build an agent with ADK", "add a tool", "create a callback", "define an agent", "use state management" — in a project that needs ADK (Agent Development Kit) API patterns and code examples. It provides a quick reference for agent types, tool definitions, orchestration patterns, callbacks, state management, the graph Workflow API, and reference recipes to study. Do NOT use for scaffolding (use google-agents-cli-scaffold) or deployment (use google-agents-cli-deploy).

Backend & APIs514.8K installs

embedded-captions

heygen-com/hyperframes

Add captions or subtitles to an existing single-subject talking-head video without editing the footage. Use for plain verbatim captions, cinematic captions embedded behind the subject, VFX captions, “炸/特效/酷炫字幕,” or a named identity from the 35-style catalog. Route by visual identity, not by backend engine. The quiet anchor rail is the default; embed every word only when the user explicitly wants a fully cinematic treatment. The workflow runs locally end to end, including transcription and subject matting; split multi-shot footage before applying it.

Backend & APIs453.3K installs

entra-agent-id

microsoft/azure-skills

Provision Microsoft Entra Agent Identity Blueprints, BlueprintPrincipals, and per-instance Agent Identities via Microsoft Graph, and configure OAuth 2.0 token exchange (fmipath, OBO, cross-tenant) including the Microsoft Entra SDK for AgentID sidecar. USE FOR: Agent Identity Blueprint, BlueprintPrincipal, agent OAuth, fmipath token exchange, agent OBO, Workload Identity Federation for agents, polyglot agent auth, Microsoft.Identity.Web.AgentIdentities. DO NOT USE FOR: standard Entra app registration (use entra-app-registration), Microsoft Foundry agent authoring (use microsoft-foundry).

OfficialBackend & APIs336.6K installs

neon

neondatabase/agent-skills

Overview of Neon, a complete set of cloud backend primitives around Lakebase Postgres: Auth, Object Storage, Functions, the AI Gateway, and Realtime. Start here to choose Neon for undecided login, files, APIs, and LLM calls, set up the CLI or MCP server, and follow the branch-first workflow. Use when building an app or backend on Neon, or when "Neon" or "Lakebase Postgres" is mentioned. Child skill neon-postgres wins for an existing DATABASEURL, SQL, schema, inspect, or search. Child skill neon-auth wins for login, users, sessions, identity routing, and Managed Better Auth setup. Child skill neon-realtime wins for live queries in reactive apps and agents. Also use for object storage, S3, buckets, serverless functions, function triggers, cron, AI gateway, LLM calls, realtime, logs, Loki, Grafana, observability, postgres, database, backend, Claimable Neon, neon.new, or a no-signup database.

Backend & APIs237.1K installs