Skip to content

Commit f630ad3

Browse files
committed
FIX: 2018 panic → 2021 compiler error
- See: #7
1 parent 0412d4a commit f630ad3

File tree

1 file changed

+17
-15
lines changed
  • Chapter01/data-types/src

1 file changed

+17
-15
lines changed

Chapter01/data-types/src/lib.rs

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
1+
#![warn(arithmetic_overflow)]
22
// Rust allows another macro type: derive. It allows to "auto-implement"
33
// supported traits. Clone, Debug, Copy are typically handy to derive.
44
#[derive(Clone, Debug, Copy)]
55
struct MyCustomStruct {
66
a: i32,
77
b: u32,
8-
pub c: f32
8+
pub c: f32,
99
}
1010

1111
// A typical Rust struct has an impl block for behavior
1212
impl MyCustomStruct {
13-
1413
// The new function is static function, and by convention a constructor
1514
pub fn new(a: i32, b: u32, c: f32) -> MyCustomStruct {
16-
MyCustomStruct {
17-
a: a, b: b, c: c
18-
}
15+
MyCustomStruct { a: a, b: b, c: c }
1916
}
2017

2118
// Instance functions feature a "self" reference as the first parameter
@@ -25,17 +22,18 @@ impl MyCustomStruct {
2522
}
2623
}
2724

28-
2925
#[cfg(test)]
3026
mod tests {
31-
use std::mem;
3227
use super::MyCustomStruct;
28+
use std::mem;
3329

3430
#[test]
3531
fn test_custom_struct() {
3632
// Rust features zero-overhead structs!
37-
assert_eq!(mem::size_of::<MyCustomStruct>(),
38-
mem::size_of::<i32>() + mem::size_of::<u32>() + mem::size_of::<f32>());
33+
assert_eq!(
34+
mem::size_of::<MyCustomStruct>(),
35+
mem::size_of::<i32>() + mem::size_of::<u32>() + mem::size_of::<f32>()
36+
);
3937

4038
let m = MyCustomStruct::new(1, 2, 3_f32);
4139
assert_eq!(m.a, 1);
@@ -49,12 +47,12 @@ mod tests {
4947
let m2 = m.clone();
5048
// We use the Debug formatter to format the struct
5149
assert_eq!(format!("{:?}", m2), "MyCustomStruct { a: 1, b: 2, c: 3.0 }");
52-
50+
5351
// This is an implicit (deep) copy, possible only with the Copy trait
5452
// Added mutability allows to change struct members
5553
let mut m3 = m;
56-
57-
// As a copy, this should not affect the other instances
54+
55+
// As a copy, this should not affect the other instances
5856
m3.a = 100;
5957

6058
// We'll make sure that the values didn't change anywhere else
@@ -87,7 +85,7 @@ mod tests {
8785
assert_eq!(a.overflowing_sub(b), (18446744073709551584, true));
8886

8987
// By default, Rust variables are immutable, add the mut qualifier
90-
// to be able to change the value
88+
// to be able to change the value
9189
let mut c = 100;
9290
c += 1;
9391
assert_eq!(c, 101);
@@ -102,6 +100,10 @@ mod tests {
102100
// This will panic since the result is going to be an unsigned
103101
// type which cannot handle negative numbers
104102
// Note: _ means ignore the result
105-
let _ = a - b;
103+
// Note: In the Rust 2021 edition, the rust compiler will catch this
104+
// error before the runtime has a chance to panic!
105+
// let _ = a - b;
106+
// ^^^^^ attempt to compute `10_u32 - 11_u32`, which would overflow
107+
let _ = a - b;
106108
}
107109
}

0 commit comments

Comments
 (0)