Skip to content

lshung/readable-rust-code

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 

Repository files navigation

Readable Rust Code

I recently came across a superb ebook called "100 Techniques for Writing Readable Rust Code" by Kanro Tomoya. This is an invaluable guide for any Rust developer aiming to write cleaner and more idiomatic code.

The original versions I found were in EPUB and a converted PDF, which can be harder to read and navigate. Additionally, the original formats lack code syntax highlighting, making the Rust examples less clear. To improve the experience, I have converted the ebook into Markdown. Furthermore, I've enhanced it by adding intuitive navigation buttons (Previous, Home, Next) at the end of every chapter for a smoother reading flow.

Heartfelt thanks to Kanro Tomoya for creating this excellent resource and sharing it with the Rust community.

Table of Contents

Number Title
1 Use pattern matching with match for clear and concise control flow
2 Leverage Option and Result types for safe and explicit error handling
3 Utilize iter, map, and filter for functional-style data processing
4 Employ enum to represent a type that can be one of several variants
5 Use trait to define shared behavior across different types
6 Name variables and functions descriptively to convey their purpose
7 Use snake_case for variable and function names for consistency
8 Prefix boolean variables with is_, has_, or can_ to indicate their nature
9 Use const and static for constants to make their immutability clear
10 Name modules and files to reflect their contents and purpose
11 Avoid abbreviations that are not universally understood
12 Use full words instead of single letters for variable names
13 Avoid using similar names for different variables to prevent confusion
14 Ensure function names clearly describe their actions or results
15 Use consistent naming conventions throughout the codebase
16 Use impl blocks to group related methods for a type
17 Leverage From and Into traits for type conversions
18 Use Cow (Clone on Write) for efficient handling of borrowed and owned data
19 Utilize Rc and Arc for reference counting and shared ownership
20 Use RefCell and Mutex for interior mutability and thread safety
21 Comment on the purpose of complex algorithms or data structures
22 Explain the reasoning behind non-obvious design decisions
23 Document the expected input and output of functions
24 Comment on the usage of unsafe code and its safety guarantees
25 Use doc comments (///) to generate documentation for public APIs
26 Keep comments up-to-date with code changes
27 Avoid redundant comments that restate the obvious
28 Use comments to explain why, not what, the code is doing
29 Be concise and to the point in your comments
30 Use TODO comments to indicate areas for future improvement
31 Use ? operator for concise error propagation
32 Leverage async and await for asynchronous programming
33 Use Box for heap allocation and dynamic dispatch
34 Utilize Vec for dynamic arrays and HashMap for key-value storage
35 Use slice and str for efficient string and array handling
36 Format code consistently using rustfmt
37 Organize code into modules and submodules logically
38 Use whitespace and indentation to enhance readability
39 Group related code together and separate different sections with blank lines
40 Avoid deeply nested code by refactoring into smaller functions
41 Use if let and while let for concise conditional checks
42 Prefer for loops over while loops for iteration
43 Use break and continue judiciously to control loop flow
44 Avoid complex nested match statements by refactoring into functions
45 Use return early to handle error cases and reduce nesting
46 Use match guards to add conditions to pattern matches
47 Leverage Result's combinators like map and and_then
48 Use unwrap_or and unwrap_or_else for default values
49 Utilize Option's combinators like map and and_then
50 Use Result and Option for error handling and optional values
51 Leverage Cow for efficient handling of borrowed and owned data
52 Use Rc and Arc for reference counting and shared ownership
53 Utilize RefCell and Mutex for interior mutability and thread safety
54 Use Box for heap allocation and dynamic dispatch
55 Leverage async and await for asynchronous programming
56 Use descriptive names for variables to indicate their purpose
57 Avoid Single-Letter Variable Names
58 Use 'let' for Declarations, 'mut' Only When Necessary
59 Group Related Variables
60 Use Constants for Unchanging Values
61 Break down complex functions into smaller, single-purpose functions
62 Use helper functions to encapsulate repetitive code
63 Avoid side effects in functions
64 Use clear and descriptive function names
65 Limit the number of parameters a function takes
66 Use impl blocks to group related methods for a type
67 Leverage From and Into traits for type conversions
68 Use Cow (Clone on Write) for efficient handling of borrowed and owned data
69 Utilize Rc and Arc for reference counting and shared ownership
70 Use RefCell and Mutex for interior mutability and thread safety
71 Break Down Large Expressions
72 Use Intermediate Variables
73 Refactor complex expressions into helper functions
74 Use parentheses to make the order of operations explicit
75 Avoid chaining too many method calls in a single line
76 Identify and extract unrelated sub-problems into separate functions
77 Use helper functions to encapsulate distinct tasks
78 Modularize code to separate concerns and improve readability
79 Use traits to define shared behavior and reduce code duplication
80 Refactor large functions into smaller, more manageable pieces
81 Use ? operator for concise error propagation
82 Leverage async and await for asynchronous programming
83 Use Box for heap allocation and dynamic dispatch
84 Utilize Vec for dynamic arrays and HashMap for key-value storage
85 Use slice and str for efficient string and array handling
86 Choose Vec for dynamic arrays when the size is unknown at compile time
87 Use HashMap for key-value pairs when fast lookup is needed
88 Leverage BTreeMap for ordered key-value storage
89 Use Option and Result for optional and error-prone values
90 Choose Rc and Arc for shared ownership and reference counting
91 Use Result for Error Handling
92 Leverage unwrap_or and unwrap_or_else for Default Values
93 Use expect with meaningful error messages for debugging
94 Handle errors at the appropriate level of abstraction
95 Use thiserror or anyhow crates for custom error types and handling
96 Write generic functions to handle multiple types
97 Use traits to define shared behavior and enable polymorphism
98 Leverage impl Trait for concise and flexible function signatures
99 Use macro_rules! to create reusable macros
100 Refactor common patterns into reusable functions or modules
101 Use crate and pub keywords to control visibility and reuse
102 Leverage Cargo workspaces to manage multiple related packages

About

100 Techniques for Writing Readable Rust Code

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published