Skip to content

headertag/zero2prod

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

zero2prod

I'm currently studying rust-lang following the direction of:

  • O'Reilly Programming Rust - Jim Blandy, Jason Orendorff & Leonara F. S. Tindall
  • Zero to Production in Rust - Luca Palmieri

Development Environment: Vim + rust-analyzer

Prerequisites

  1. Rust toolchain via rustup:

    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

    Verify:

    rustc --version
    cargo --version
  2. rust-analyzer (the Rust LSP server):

    rustup component add rust-analyzer

    Verify:

    rust-analyzer --version
  3. clippy and rustfmt:

    rustup component add clippy rustfmt
  4. Vim 9.0+ with +channel and +job support (check with vim --version).

Install the LSP Plugin

We use yegappan/lsp, a Vim9-native LSP client. Install it with Vim's built-in package manager — no external plugin manager needed:

mkdir -p ~/.vim/pack/plugins/start
git clone https://github.com/yegappan/lsp.git ~/.vim/pack/plugins/start/lsp

Configure Vim

Add the following to ~/.vimrc:

" General settings
set nocompatible
syntax on
filetype plugin indent on
set number
set relativenumber
set signcolumn=yes
set updatetime=300
set completeopt=menuone,popup,noinsert,noselect

" Rust file settings
autocmd FileType rust setlocal tabstop=4 shiftwidth=4 expandtab

" LSP configuration (yegappan/lsp with rust-analyzer)
let lspServers = [
    \   #{
    \     name: 'rust-analyzer',
    \     filetype: ['rust'],
    \     path: expand('~/.rustup/toolchains/stable-aarch64-apple-darwin/bin/rust-analyzer'),
    \     args: [],
    \     syncInit: v:true,
    \   }
    \ ]

autocmd VimEnter * call LspAddServer(lspServers)

" LSP keybindings
autocmd FileType rust nnoremap gd :LspGotoDefinition<CR>
autocmd FileType rust nnoremap gr :LspShowReferences<CR>
autocmd FileType rust nnoremap gi :LspGotoImpl<CR>
autocmd FileType rust nnoremap K :LspHover<CR>
autocmd FileType rust nnoremap <leader>rn :LspRename<CR>
autocmd FileType rust nnoremap <leader>ca :LspCodeAction<CR>
autocmd FileType rust nnoremap [d :LspDiagPrev<CR>
autocmd FileType rust nnoremap ]d :LspDiagNext<CR>
autocmd FileType rust nnoremap <leader>d :LspDiagCurrent<CR>

Verify the Setup

From the command line:

# Project compiles
cargo check

# Clippy passes
cargo clippy

# Formatting is correct
cargo fmt --check

# rust-analyzer can analyze the project
rust-analyzer analysis-stats .

From inside Vim:

  1. Open a Rust file: vim src/main.rs
  2. Verify LSP is running: :LspServerStatus — should show rust-analyzer as running.
  3. Test go-to-definition: place cursor on println! and press gd.
  4. Test hover: press K on any symbol to see type info.
  5. Test diagnostics: introduce a type error and check that [d / ]d navigate between errors.

Keybinding Reference

Key Action
gd Go to definition
gr Show references
gi Go to implementation
K Hover documentation
<leader>rn Rename symbol
<leader>ca Code action
[d / ]d Previous / next diagnostic
<leader>d Show current diagnostic
:LspDiagShow Open quickfix list with all diagnostics
:LspDiagCurrent Get info on the highlighted error

Hello, World! Why Rust?

Language Executable Size (M1) What is actually inside the file? The "Why Rust?" Perspective
Assembly (ARM64) 16 K Raw CPU instructions wrapped in an empty 16KB Mach-O envelope. Ultimate Control: Absolute bare metal. Zero abstractions, zero safety nets. You are manually pushing bits.
C 33 K CPU instructions + a tiny dynamic link to the OS's libc. Fast but Unsafe: Tiny and lightning-fast, but relies on the OS. You are entirely responsible for manual memory management (which often leads to security vulnerabilities and crashes).
Rust 435 K CPU instructions + Rust's statically linked standard library. The Sweet Spot: It statically links its own standard lib (hence 435K instead of 33K) so it can run independently anywhere. It gives you the memory safety of Python/Go, but with zero garbage collector and zero heavy runtime.
Go 1.9 M CPU instructions + standard lib + the Go Runtime (Garbage Collector). Safe but Heavy: Great developer experience and built-in concurrency, but you pay a mandatory ~1.5MB "tax" for the garbage collector and runtime scheduler.
Python (PyInstaller) 6.5 M The Python C-interpreter + standard lib + your script. Ultimate Convenience: Extremely easy to write, but massive overhead. You are shipping an entire software engine just to execute a text file.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages