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