Skip to content

Commit 21f5ac5

Browse files
committed
fix: wrapped in main
1 parent 9caf231 commit 21f5ac5

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/coding-guidelines/values.rst

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ Values
5858

5959
use std::mem::MaybeUninit;
6060

61+
# fn main() {
6162
let x: u32 = unsafe { MaybeUninit::uninit().assume_init() }; // UB
63+
# }
6264

6365
.. compliant_example::
6466
:id: compl_ex_Ke869nSXuShU
@@ -74,22 +76,26 @@ Values
7476
bytes: [u8; 4],
7577
}
7678

79+
# fn main() {
7780
let u = U { bytes: [0xFF, 0xEE, 0xDD, 0xCC] };
7881
let n = unsafe { u.n }; // OK — all bit patterns valid for u32
82+
# }
7983

8084
.. compliant_example::
8185
:id: compl_ex_Ke869nSXuShV
8286
:status: draft
8387

84-
This compliant example calls the ``write`` function to fully initialize low-level memory.
88+
This compliant example calles the ``write`` function to fully initialize low-level memory.
8589

8690
.. rust-example::
8791

8892
use std::mem::MaybeUninit;
8993

94+
# fn main() {
9095
let mut x = MaybeUninit::<u64>::uninit();
9196
x.write(42);
9297
let val = unsafe { x.assume_init() }; // OK — value was fully initialized
98+
# }
9399

94100
.. non_compliant_example::
95101
:id: non_compl_ex_Qb5GqYTP6db2
@@ -103,7 +109,9 @@ Values
103109

104110
use std::mem::MaybeUninit;
105111

112+
# fn main() {
106113
let r: &u32 = unsafe { MaybeUninit::uninit().assume_init() }; // UB — invalid reference
114+
# }
107115

108116
.. non_compliant_example::
109117
:id: non_compl_ex_Qb5GqYTP6db4
@@ -117,7 +125,9 @@ Values
117125

118126
use std::mem::MaybeUninit;
119127

128+
# fn main() {
120129
let p: *const u32 = unsafe { MaybeUninit::uninit().assume_init() }; // UB
130+
# }
121131
122132
.. non_compliant_example::
123133
:id: non_compl_ex_Qb5GqYTP6db5
@@ -129,8 +139,10 @@ Values
129139

130140
use std::mem::MaybeUninit;
131141

142+
# fn main() {
132143
let mut arr: [MaybeUninit<u8>; 4] = unsafe { MaybeUninit::uninit().assume_init() };
133144
let a = unsafe { std::mem::transmute::<_, [u8; 4]>(arr) }; // UB — not all elements initialized
145+
# }
134146

135147
.. compliant_example::
136148
:id: compl_ex_Ke869nSXuShT
@@ -145,8 +157,10 @@ Values
145157
y: f32,
146158
}
147159

160+
# fn main() {
148161
let u = U { x: 123 }; // write to one field
149162
let f = unsafe { u.y }; // reading the other field is allowed
163+
# }
150164

151165
.. non_compliant_example::
152166
:id: non_compl_ex_Qb5GqYTP6db3
@@ -164,8 +178,10 @@ Values
164178
x: u8,
165179
}
166180

181+
# fn main() {
167182
let u = U { x: 255 }; // 255 is not a valid bool representation
168183
let b = unsafe { u.b }; // UB — invalid bool
184+
# }
169185

170186
.. compliant_example::
171187
:id: compl_ex_Ke869nSXuShW

0 commit comments

Comments
 (0)