mirror of
https://github.com/leonardomso/rust-skills.git
synced 2026-09-14 19:33:21 +08:00
0016d5cfb2
Includes rules for: - Ownership and borrowing patterns - Error handling with thiserror/anyhow - Memory management and allocation - API design following Rust guidelines - Async/Tokio patterns - Performance optimization - Naming conventions - Type safety - Testing strategies - Documentation standards - Project structure - Linting configuration - Common anti-patterns to avoid
1.8 KiB
1.8 KiB
name-variants-camel
Use
UpperCamelCasefor enum variants
Why It Matters
Enum variants follow the same naming convention as types—UpperCamelCase. This distinguishes them from fields, variables, and functions. The compiler warns on violations, and consistent naming helps readers instantly recognize variant names.
Bad
enum Status {
pending, // warning: variant `pending` should have an upper camel case name
in_progress, // warning
COMPLETED, // Not idiomatic
}
enum Color {
RED, // Screaming case - not Rust style
GREEN,
BLUE,
}
Good
enum Status {
Pending,
InProgress,
Completed,
Failed,
}
enum Color {
Red,
Green,
Blue,
Custom(u8, u8, u8),
}
enum HttpMethod {
Get,
Post,
Put,
Delete,
Patch,
}
Variants with Data
enum Message {
// Unit variant
Quit,
// Tuple variant
Move(i32, i32),
// Struct variant
Write { text: String },
// Named fields
ChangeColor {
red: u8,
green: u8,
blue: u8,
},
}
Variant Naming Tips
// Be specific
enum Error {
NotFound, // Good: specific
PermissionDenied, // Good: specific
Error, // Bad: vague
}
// Avoid redundant type name in variant
enum ConnectionState {
Connected, // Good
Disconnected, // Good
ConnectionError, // Bad: redundant "Connection"
}
// Use None/Some pattern for Option-like enums
enum MaybeValue<T> {
Some(T),
None,
}
See Also
- name-types-camel - Type naming
- api-non-exhaustive - Forward-compatible enums
- type-enum-states - State machine enums