April 2026
Intermediate
631 pages
16h 20m
English
Sometimes, a large, poorly designed struct can lead to borrowing issues. While individual fields might be borrowable independently, borrowing the struct as a whole can block further borrows or block usage of the struct, ultimately preventing other parts of the code from accessing it.
Consider struct A defined in Listing 12.19.
struct A { f1: u32, f2: u32, f3: u32,}
Listing 12.19 Definition of Struct A Containing Three Fields
Let’s define a few functions that will use the struct instance. Listing 12.20 shows the definitions of these functions.
fn fn1(a: &mut A) -> &u32 { &a.f2}fn fn2(a: &mut A) -> u32 { a.f1 + a.f3}fn fn3(a: &mut A) { let x = fn1(a); let y = fn2(a);}
Listing 12.20 Some Functions Defined on ...
Read now
Unlock full access