Rust Development Guide#
This guide covers Rust-specific features and workflows in this Neovim configuration.
Overview#
The Rust setup is based on LazyVim’s Rust extras and includes:
- rustaceanvim - Advanced Rust language support
- crates.nvim - Cargo.toml dependency management
- rust-analyzer - Powerful LSP for Rust
- codelldb - Debugger for Rust applications
- neotest - Test runner integration
Getting Started with Rust#
Prerequisites#
Make sure you have Rust installed:
# Install Rust via rustup
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# Verify installation
rustc --version
cargo --versionCreating a New Rust Project#
# Create a new binary project
cargo new my-project
cd my-project
# Or create a library
cargo new --lib my-libraryThen open the project in Neovim:
nvim .Rust-Specific Features#
rust-analyzer LSP#
rust-analyzer provides advanced IDE features:
- All cargo features enabled - Full project analysis
- Build scripts support - Understands build.rs files
- Clippy integration - Linting on save
- Procedural macros - Expanded and analyzed
- Smart file watching - Excludes common directories (.git, target, etc.)
Code Actions#
Press <Space>cR to access Rust-specific code actions:
- Add missing derive traits
- Fill match arms
- Import items
- Generate documentation
- Convert to guarded return
- Inline variable/function
- And many more!
Debuggables#
Press <Space>dr to open the debuggables menu, which shows:
- Binaries - Main executables in your project
- Tests - Individual test functions
- Examples - Example programs
Select one to start debugging immediately with codelldb.
Cargo.toml Support#
When editing Cargo.toml files, crates.nvim provides:
Dependency Completion#
Start typing a crate name under [dependencies]:
[dependencies]
serde = # <-- Completion will suggest latest versionInline Documentation#
Hover over a dependency to see:
- Crate description
- Latest version
- Documentation link
- Repository link
Version Checking#
Get inline diagnostics for:
- Outdated dependencies
- Incompatible versions
- Available updates
Running Tests#
Rust tests integrate with neotest via the rustaceanvim adapter:
# Run all tests
:!cargo test
# Run specific test
:!cargo test test_name
# Run tests in a specific module
:!cargo test module_nameYou can also use the debuggables menu (<Space>dr) to run/debug individual tests.
Common Workflows#
Adding a Dependency#
- Open
Cargo.toml - Navigate to
[dependencies]section - Start typing the crate name
- Use completion (
<C-Space>) to select - Save the file
- Run
:!cargo buildto download
Implementing a Trait#
- Place cursor on type that needs trait
- Press
<Space>cRfor code actions - Select “Implement missing members”
- Stubs will be generated automatically
Running and Debugging#
- Press
<Space>drto open debuggables - Select your binary or test
- Debugger starts with breakpoints active
- Use standard debug keybindings:
<Space>dc- Continue<Space>dn- Step over<Space>di- Step into<Space>do- Step out
Exploring Documentation#
- Place cursor on any item (function, type, etc.)
- Press
Kto show inline documentation - Press
gdto jump to definition - Press
grto find all references
Refactoring#
- Place cursor on symbol to rename
- Press
<Space>rn - Type new name
- Press Enter
- All references update across the project
Rust-Specific Settings#
The rust-analyzer is configured with:
cargo = {
allFeatures = true, -- Enable all features
buildScripts = { enable = true }, -- Support build.rs
}
checkOnSave = {
command = 'clippy', -- Use clippy for linting
extraArgs = { '--no-deps' }, -- Only check your code
}
procMacro = {
enable = true, -- Expand procedural macros
}These settings provide comprehensive project analysis while keeping performance optimal.
Tips and Tricks#
Quick Build#
Set up a keybinding to build quickly:
:!cargo buildOr run it in the background with ToggleTerm.
Clippy Linting#
Get instant feedback from clippy:
# Manual run
:!cargo clippy
# Or it runs automatically on save via LSPFormat on Save#
rust-analyzer automatically formats code using rustfmt when you save.
Expand Macros#
Use <Space>cR and select “Expand macro recursively” to see what procedural macros generate.
View Cargo Tree#
Check your dependency tree:
:!cargo treeTroubleshooting#
rust-analyzer Not Working#
- Check it’s installed:
:Masonand verify rust_analyzer is installed - Check LSP status:
:LspInfo - Restart LSP:
<Space>rs - Full restart: Close Neovim and reopen
Slow Performance#
If rust-analyzer is slow on large projects:
- Disable some features in your settings
- Use
cargo checkinstead ofcargo build - Add workspace folders to exclude in settings
Debugging Not Starting#
- Verify codelldb is installed:
:Mason - Check DAP configuration:
:lua print(vim.inspect(require('dap').configurations.rust)) - Make sure you’re running debug build (not release)
Resources#
- Rust Book - Official Rust guide
- Rust by Example - Learn by examples
- rust-analyzer Manual - LSP documentation
- rustaceanvim Docs - Plugin documentation
- crates.nvim Docs - Cargo.toml helper docs
For general keybindings, see Quick Reference