Rust Default Values for Maintainability

TL;DR The Default trait can enhance the maintenability of your code. Default values for common types are listed at the end. A PR Review Recently, while reviewing a PR1, I noticed that part of the patch was introducing a new field to a struct: 1diff --git a/src/lib.rs b/src/lib.rs 2index eba9a3a..8619e06 100644 3--- a/src/lib.rs 4+++ b/src/lib.rs 5@@ -106,8 +108,9 @@ use std::{ 6 #[derive(Debug, PartialEq, Clone)] 7 pub struct M<'u> { 8 up: &'u str, 9 down: Option<&'u str>, 10+ foreign_key_check: bool, 11 } 12 13 impl<'u> M<'u> { 14@@ -137,8 +140,9 @@ impl<'u> M<'u> { 15 pub const fn up(sql: &'u str) -> Self { 16 Self { 17 up: sql, 18 down: None, 19+ foreign_key_check: false, 20 } 21 } That prompted me to reflect on the code I had initially written....

June 25, 2022 · 5 min