File tree Expand file tree Collapse file tree 1 file changed +6
-2
lines changed Expand file tree Collapse file tree 1 file changed +6
-2
lines changed Original file line number Diff line number Diff line change @@ -28,16 +28,20 @@ fn compute(input: &u32, output: &mut u32) {
28
28
if * input > 5 {
29
29
* output *= 2 ;
30
30
}
31
+ // remember that `output` will be `2` if `input > 10`
31
32
}
32
33
```
33
34
34
35
We would * like* to be able to optimize it to the following function:
35
36
36
37
``` rust
37
38
fn compute (input : & u32 , output : & mut u32 ) {
38
- let cached_input = * input ; // keep *input in a register
39
+ let cached_input = * input ; // keep ` *input` in a register
39
40
if cached_input > 10 {
40
- * output = 2 ; // x > 10 implies x > 5, so double and exit immediately
41
+ // If the input is greater than 10, the previous code would set the output to 1 and then double it,
42
+ // resulting in an output of 2 (because `>10` implies `>5`).
43
+ // Here, we avoid the double assignment and just set it directly to 2.
44
+ * output = 2 ;
41
45
} else if cached_input > 5 {
42
46
* output *= 2 ;
43
47
}
You can’t perform that action at this time.
0 commit comments