From 900561e9f7f3add7d97e69c62c503a2b9c9c2e3c Mon Sep 17 00:00:00 2001
From: Dmitry Sorokin <a@dimir.name>
Date: Sat, 20 Apr 2024 23:08:03 +0300
Subject: [PATCH 1/3] Wrap long lines in code blocks;

---
 src/memory-management/review.md     | 3 ++-
 src/unsafe-rust/unsafe-functions.md | 4 +++-
 src/user-defined-types/enums.md     | 6 +++++-
 3 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/memory-management/review.md b/src/memory-management/review.md
index 04c719a8dbff..6e47d45d0cd8 100644
--- a/src/memory-management/review.md
+++ b/src/memory-management/review.md
@@ -65,7 +65,8 @@ fn main() {
     // String provides no guarantees about its layout, so this could lead to
     // undefined behavior.
     unsafe {
-        let (capacity, ptr, len): (usize, usize, usize) = std::mem::transmute(s1);
+        let (capacity, ptr, len): (usize, usize, usize) = 
+            std::mem::transmute(s1);
         println!("capacity = {capacity}, ptr = {ptr:#x}, len = {len}");
     }
 }
diff --git a/src/unsafe-rust/unsafe-functions.md b/src/unsafe-rust/unsafe-functions.md
index 6503506f8fc7..81ac46a6898a 100644
--- a/src/unsafe-rust/unsafe-functions.md
+++ b/src/unsafe-rust/unsafe-functions.md
@@ -25,7 +25,9 @@ fn main() {
         println!("emoji: {}", emojis.get_unchecked(7..11));
     }
 
-    println!("char count: {}", count_chars(unsafe { emojis.get_unchecked(0..7) }));
+    println!(
+        "char count: {}", 
+        count_chars(unsafe { emojis.get_unchecked(0..7) }));
 
     // SAFETY: `abs` doesn't deal with pointers and doesn't have any safety
     // requirements.
diff --git a/src/user-defined-types/enums.md b/src/user-defined-types/enums.md
index dc34b80ffbf6..fefbaa6d1fc0 100644
--- a/src/user-defined-types/enums.md
+++ b/src/user-defined-types/enums.md
@@ -88,7 +88,11 @@ Rust has several optimizations it can employ to make enums take up less space.
 
   macro_rules! dbg_bits {
       ($e:expr, $bit_type:ty) => {
-          println!("- {}: {:#x}", stringify!($e), transmute::<_, $bit_type>($e));
+          println!(
+              "- {}: {:#x}", 
+              stringify!($e), 
+              transmute::<_, $bit_type>($e)
+          );
       };
   }
 

From 4a235ee952d1caf2650a2f7435a3efdccf73d146 Mon Sep 17 00:00:00 2001
From: Dmitry Sorokin <a@dimir.name>
Date: Sun, 21 Apr 2024 21:32:52 +0300
Subject: [PATCH 2/3] Remove horizontal scrolling in the rust code blocks

* Reduce `max_width` for `rustfmt`: 85 -> 81 - to fit into blocks with line numbers;
* Apply `rustfmt` for spilling code chunks;
* Reorganize long `use` statement;
* Manually wrap long strings in macros;
---
 rustfmt.toml                                      |  3 ++-
 src/android/aidl/birthday_service/src/client.rs   | 10 ++++++----
 src/android/aidl/birthday_service/src/lib.rs      |  8 +++++---
 src/android/aidl/birthday_service/src/server.rs   |  4 +++-
 src/android/aidl/example-service/client.md        |  4 +++-
 src/android/aidl/example-service/service.md       |  4 +++-
 .../examples/src/bin/typestate.rs                 | 12 ++++++++----
 src/borrowing/exercise.md                         |  5 ++++-
 src/error-handling/exercise.rs                    |  6 ++++--
 src/iterators/intoiterator.md                     |  3 ++-
 src/lifetimes/exercise.md                         |  5 ++++-
 src/lifetimes/exercise.rs                         |  4 +++-
 src/memory-management/exercise.rs                 |  6 ++++--
 src/testing/exercise.rs                           | 14 ++++++++++----
 src/unsafe-rust/exercise.rs                       |  3 ++-
 third_party/rust-on-exercism/health-statistics.rs | 15 ++++++++-------
 16 files changed, 71 insertions(+), 35 deletions(-)

diff --git a/rustfmt.toml b/rustfmt.toml
index 131d9d6bc4f0..dbb46c367ab9 100644
--- a/rustfmt.toml
+++ b/rustfmt.toml
@@ -3,6 +3,7 @@ imports_granularity = "module"
 wrap_comments = true
 
 # The code blocks get a scrollbar if they are wider than this.
-max_width = 85
+max_width = 81
+
 # Allow all constructs to take up max_width columns.
 use_small_heuristics = "Max"
diff --git a/src/android/aidl/birthday_service/src/client.rs b/src/android/aidl/birthday_service/src/client.rs
index 261fcb61405f..a24741a4a27d 100644
--- a/src/android/aidl/birthday_service/src/client.rs
+++ b/src/android/aidl/birthday_service/src/client.rs
@@ -13,11 +13,13 @@
 // limitations under the License.
 
 //! Birthday service.
-use com_example_birthdayservice::aidl::com::example::birthdayservice::BirthdayInfo::BirthdayInfo;
-use com_example_birthdayservice::aidl::com::example::birthdayservice::IBirthdayInfoProvider::{
-    BnBirthdayInfoProvider, IBirthdayInfoProvider,
+use com_example_birthdayservice::aidl::com::example::birthdayservice::{
+    BirthdayInfo::BirthdayInfo,
+    IBirthdayInfoProvider::{
+        BnBirthdayInfoProvider, IBirthdayInfoProvider,
+    },
+    IBirthdayService::IBirthdayService
 };
-use com_example_birthdayservice::aidl::com::example::birthdayservice::IBirthdayService::IBirthdayService;
 use com_example_birthdayservice::binder::{self, BinderFeatures, ParcelFileDescriptor};
 use std::error::Error;
 use std::fs::File;
diff --git a/src/android/aidl/birthday_service/src/lib.rs b/src/android/aidl/birthday_service/src/lib.rs
index 1a6e5f32bf25..8ff6ef1e667e 100644
--- a/src/android/aidl/birthday_service/src/lib.rs
+++ b/src/android/aidl/birthday_service/src/lib.rs
@@ -13,9 +13,11 @@
 // limitations under the License.
 
 //! Implementation of the `IBirthdayService` AIDL interface.
-use com_example_birthdayservice::aidl::com::example::birthdayservice::IBirthdayInfoProvider::IBirthdayInfoProvider;
-use com_example_birthdayservice::aidl::com::example::birthdayservice::IBirthdayService::IBirthdayService;
-use com_example_birthdayservice::aidl::com::example::birthdayservice::BirthdayInfo::BirthdayInfo;
+use com_example_birthdayservice::aidl::com::example::birthdayservice::{
+    IBirthdayInfoProvider::IBirthdayInfoProvider,
+    IBirthdayService::IBirthdayService,
+    BirthdayInfo::BirthdayInfo
+};
 use com_example_birthdayservice::binder::{self, ParcelFileDescriptor, SpIBinder, Strong};
 use std::fs::File;
 use std::io::Read;
diff --git a/src/android/aidl/birthday_service/src/server.rs b/src/android/aidl/birthday_service/src/server.rs
index 8393c63f35a7..d1adc50d0e66 100644
--- a/src/android/aidl/birthday_service/src/server.rs
+++ b/src/android/aidl/birthday_service/src/server.rs
@@ -15,7 +15,9 @@
 // ANCHOR: main
 //! Birthday service.
 use birthdayservice::BirthdayService;
-use com_example_birthdayservice::aidl::com::example::birthdayservice::IBirthdayService::BnBirthdayService;
+use com_example_birthdayservice::aidl::com::example::birthdayservice::{
+    IBirthdayService::BnBirthdayService
+};
 use com_example_birthdayservice::binder;
 
 const SERVICE_IDENTIFIER: &str = "birthdayservice";
diff --git a/src/android/aidl/example-service/client.md b/src/android/aidl/example-service/client.md
index 4213496d7cf2..af4a68464f51 100644
--- a/src/android/aidl/example-service/client.md
+++ b/src/android/aidl/example-service/client.md
@@ -5,7 +5,9 @@ Finally, we can create a Rust client for our new service.
 _birthday_service/src/client.rs_:
 
 ```rust,ignore
-use com_example_birthdayservice::aidl::com::example::birthdayservice::IBirthdayService::IBirthdayService;
+use com_example_birthdayservice::aidl::com::example::birthdayservice::{
+    IBirthdayService::IBirthdayService
+};
 use com_example_birthdayservice::binder;
 
 {{#include ../birthday_service/src/client.rs:main}}
diff --git a/src/android/aidl/example-service/service.md b/src/android/aidl/example-service/service.md
index d9b1c8f8b4eb..aea6321781d0 100644
--- a/src/android/aidl/example-service/service.md
+++ b/src/android/aidl/example-service/service.md
@@ -5,7 +5,9 @@ We can now implement the AIDL service:
 _birthday_service/src/lib.rs_:
 
 ```rust,ignore
-use com_example_birthdayservice::aidl::com::example::birthdayservice::IBirthdayService::IBirthdayService;
+use com_example_birthdayservice::aidl::com::example::birthdayservice::{
+    IBirthdayService::IBirthdayService
+};
 use com_example_birthdayservice::binder;
 
 {{#include ../birthday_service/src/lib.rs:IBirthdayService}}
diff --git a/src/bare-metal/microcontrollers/examples/src/bin/typestate.rs b/src/bare-metal/microcontrollers/examples/src/bin/typestate.rs
index 71071aff700f..8eda33d9773f 100644
--- a/src/bare-metal/microcontrollers/examples/src/bin/typestate.rs
+++ b/src/bare-metal/microcontrollers/examples/src/bin/typestate.rs
@@ -40,13 +40,17 @@ fn main() -> ! {
         // ...
     }
     let mut pin_output: P0_01<Output<OpenDrain>> = pin_input
-        .into_open_drain_output(OpenDrainConfig::Disconnect0Standard1, Level::Low);
+        .into_open_drain_output(
+            OpenDrainConfig::Disconnect0Standard1,
+            Level::Low,
+        );
     pin_output.set_high().unwrap();
     // pin_input.is_high(); // Error, moved.
 
-    let _pin2: P0_02<Output<OpenDrain>> = gpio0
-        .p0_02
-        .into_open_drain_output(OpenDrainConfig::Disconnect0Standard1, Level::Low);
+    let _pin2: P0_02<Output<OpenDrain>> = gpio0.p0_02.into_open_drain_output(
+        OpenDrainConfig::Disconnect0Standard1,
+        Level::Low,
+    );
     let _pin3: P0_03<Output<PushPull>> =
         gpio0.p0_03.into_push_pull_output(Level::Low);
 
diff --git a/src/borrowing/exercise.md b/src/borrowing/exercise.md
index a6b9cd3a2e88..160aa9f43cf2 100644
--- a/src/borrowing/exercise.md
+++ b/src/borrowing/exercise.md
@@ -16,7 +16,10 @@ method:
 {{#include ../../third_party/rust-on-exercism/health-statistics.rs:setup}}
 
 {{#include ../../third_party/rust-on-exercism/health-statistics.rs:User_visit_doctor}}
-        todo!("Update a user's statistics based on measurements from a visit to the doctor")
+        todo!(
+            "Update a user's statistics based on measurements from a visit to \
+                the doctor"
+        )
     }
 }
 
diff --git a/src/error-handling/exercise.rs b/src/error-handling/exercise.rs
index 6b4511112d8f..3a052ad3a3b8 100644
--- a/src/error-handling/exercise.rs
+++ b/src/error-handling/exercise.rs
@@ -73,7 +73,8 @@ impl<'a> Iterator for Tokenizer<'a> {
             }
             'a'..='z' => {
                 let mut ident = String::from(c);
-                while let Some(c @ ('a'..='z' | '_' | '0'..='9')) = self.0.peek() {
+                while let Some(c @ ('a'..='z' | '_' | '0'..='9')) = self.0.peek()
+                {
                     ident.push(*c);
                     self.0.next();
                 }
@@ -160,7 +161,8 @@ impl<'a> Iterator for Tokenizer<'a> {
             }
             'a'..='z' => {
                 let mut ident = String::from(c);
-                while let Some(c @ ('a'..='z' | '_' | '0'..='9')) = self.0.peek() {
+                while let Some(c @ ('a'..='z' | '_' | '0'..='9')) = self.0.peek()
+                {
                     ident.push(*c);
                     self.0.next();
                 }
diff --git a/src/iterators/intoiterator.md b/src/iterators/intoiterator.md
index aeb22c243d15..afc76b8f6147 100644
--- a/src/iterators/intoiterator.md
+++ b/src/iterators/intoiterator.md
@@ -48,7 +48,8 @@ impl Iterator for GridIter {
 }
 
 fn main() {
-    let grid = Grid { x_coords: vec![3, 5, 7, 9], y_coords: vec![10, 20, 30, 40] };
+    let grid = 
+        Grid { x_coords: vec![3, 5, 7, 9], y_coords: vec![10, 20, 30, 40] };
     for (x, y) in grid {
         println!("point = {x}, {y}");
     }
diff --git a/src/lifetimes/exercise.md b/src/lifetimes/exercise.md
index c742abf9a592..c34c15d715b7 100644
--- a/src/lifetimes/exercise.md
+++ b/src/lifetimes/exercise.md
@@ -49,7 +49,10 @@ What remains for you is to implement the `parse_field` function and the
 
 
 {{#include exercise.rs:parse_field }}
-        _ => todo!("Based on the wire type, build a Field, consuming as many bytes as necessary.")
+        _ => todo!(
+            "Based on the wire type, build a Field, consuming as many bytes as \
+             necessary."
+        )
     };
     todo!("Return the field, and any un-consumed bytes.")
 }
diff --git a/src/lifetimes/exercise.rs b/src/lifetimes/exercise.rs
index ab73d9ce1cd5..b06d1e3c95b8 100644
--- a/src/lifetimes/exercise.rs
+++ b/src/lifetimes/exercise.rs
@@ -170,7 +170,9 @@ fn parse_field(data: &[u8]) -> Result<(Field, &[u8]), Error> {
 /// the message.
 ///
 /// The entire input is consumed.
-fn parse_message<'a, T: ProtoMessage<'a>>(mut data: &'a [u8]) -> Result<T, Error> {
+fn parse_message<'a, T: ProtoMessage<'a>>(
+    mut data: &'a [u8],
+) -> Result<T, Error> {
     let mut result = T::default();
     while !data.is_empty() {
         let parsed = parse_field(data)?;
diff --git a/src/memory-management/exercise.rs b/src/memory-management/exercise.rs
index 44251c5040b1..e5d536f0f8c5 100644
--- a/src/memory-management/exercise.rs
+++ b/src/memory-management/exercise.rs
@@ -114,8 +114,10 @@ impl PackageBuilder {
 fn main() {
     let base64 = PackageBuilder::new("base64").version("0.13").build();
     println!("base64: {base64:?}");
-    let log =
-        PackageBuilder::new("log").version("0.4").language(Language::Rust).build();
+    let log = PackageBuilder::new("log")
+        .version("0.4")
+        .language(Language::Rust)
+        .build();
     println!("log: {log:?}");
     let serde = PackageBuilder::new("serde")
         .authors(vec!["djmitche".into()])
diff --git a/src/testing/exercise.rs b/src/testing/exercise.rs
index 385eb9bd4301..24d925057585 100644
--- a/src/testing/exercise.rs
+++ b/src/testing/exercise.rs
@@ -24,8 +24,11 @@ pub fn luhn(cc_number: &str) -> bool {
         if let Some(digit) = c.to_digit(10) {
             if double {
                 let double_digit = digit * 2;
-                sum +=
-                    if double_digit > 9 { double_digit - 9 } else { double_digit };
+                sum += if double_digit > 9 {
+                    double_digit - 9
+                } else {
+                    double_digit
+                };
             } else {
                 sum += digit;
             }
@@ -50,8 +53,11 @@ pub fn luhn(cc_number: &str) -> bool {
             digits += 1;
             if double {
                 let double_digit = digit * 2;
-                sum +=
-                    if double_digit > 9 { double_digit - 9 } else { double_digit };
+                sum += if double_digit > 9 {
+                    double_digit - 9
+                } else {
+                    double_digit
+                };
             } else {
                 sum += digit;
             }
diff --git a/src/unsafe-rust/exercise.rs b/src/unsafe-rust/exercise.rs
index 0eb0cc6c7752..d0a91c7f88d7 100644
--- a/src/unsafe-rust/exercise.rs
+++ b/src/unsafe-rust/exercise.rs
@@ -23,7 +23,8 @@ mod ffi {
     #[repr(C)]
     pub struct DIR {
         _data: [u8; 0],
-        _marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
+        _marker:
+            core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
     }
 
     // Layout according to the Linux man page for readdir(3), where ino_t and
diff --git a/third_party/rust-on-exercism/health-statistics.rs b/third_party/rust-on-exercism/health-statistics.rs
index 52ab7ebb72e9..d0f3b0e008f5 100644
--- a/third_party/rust-on-exercism/health-statistics.rs
+++ b/third_party/rust-on-exercism/health-statistics.rs
@@ -38,9 +38,10 @@ impl User {
             visit_count: self.visit_count as u32,
             height_change: measurements.height - self.height,
             blood_pressure_change: match self.last_blood_pressure {
-                Some(lbp) => {
-                    Some((bp.0 as i32 - lbp.0 as i32, bp.1 as i32 - lbp.1 as i32))
-                }
+                Some(lbp) => Some((
+                    bp.0 as i32 - lbp.0 as i32,
+                    bp.1 as i32 - lbp.1 as i32,
+                )),
                 None => None,
             },
         };
@@ -62,14 +63,14 @@ fn main() {
 fn test_visit() {
     let mut bob = User::new(String::from("Bob"), 32, 155.2);
     assert_eq!(bob.visit_count, 0);
-    let report =
-        bob.visit_doctor(Measurements { height: 156.1, blood_pressure: (120, 80) });
+    let report = bob
+        .visit_doctor(Measurements { height: 156.1, blood_pressure: (120, 80) });
     assert_eq!(report.patient_name, "Bob");
     assert_eq!(report.visit_count, 1);
     assert_eq!(report.blood_pressure_change, None);
 
-    let report =
-        bob.visit_doctor(Measurements { height: 156.1, blood_pressure: (115, 76) });
+    let report = bob
+        .visit_doctor(Measurements { height: 156.1, blood_pressure: (115, 76) });
 
     assert_eq!(report.visit_count, 2);
     assert_eq!(report.blood_pressure_change, Some((-5, -4)));

From 2967280f89d4d836d52388f66eb919f5045d8d95 Mon Sep 17 00:00:00 2001
From: Dmitry Sorokin <a@dimir.name>
Date: Sun, 21 Apr 2024 23:12:35 +0300
Subject: [PATCH 3/3] Rollback `max_width` for `rustfmt`: 81 -> 85 - too many
 false positives

---
 rustfmt.toml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/rustfmt.toml b/rustfmt.toml
index dbb46c367ab9..9c257d0ca02a 100644
--- a/rustfmt.toml
+++ b/rustfmt.toml
@@ -3,7 +3,7 @@ imports_granularity = "module"
 wrap_comments = true
 
 # The code blocks get a scrollbar if they are wider than this.
-max_width = 81
+max_width = 85
 
 # Allow all constructs to take up max_width columns.
 use_small_heuristics = "Max"