4 ms·
Example for skipping return keyword in case of early return is just wrong. It can be made to work if you have if x > 0 { x } else { x + 1 } but then it isn't ea
by Karliss 7y ago
Example for skipping return keyword in case of early return is just wrong. It can be made to work if you have
if x > 0 { x } else { x + 1 } but then it isn't early return anymore.
- wycy 7y agoYeah the early return example is wrong. Demonstration code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn%20main()%20%7B%0A%20%20%20%20println!(%22foo(3)%3D%7B%7D%22%2Cfoo(3))%3B%0A%20%20%20%20println!(%22foo(-3)%3D%7B%7D%22%2Cfoo(-3))%3B%0A%7D%0A%0Afn%20foo(x%3A%20i32)%20-%3E%20i32%20%7B%0A%20%20%20%20if%20x%20%3E%200%20%7B%0A%20%20%20%20%20%20%20%20return%20x%3B%20%0A%20%20%20%20%7D%0A%20%20%20%20x%20%2B%201%0A%7D https://play.rust-lang.org/?version=stable&mode=debug&editio... Try removing the `return` and the semicolon.
- bartkk 7y agoThank you. We fixed this unfortunate example, and added some more explanation.