4 ms·
> First of all, they kind of dodge this later by saying "Array indexing must be properly tested" (else it can panic) -- everyone thinks they write code which is
by masklinn 7y ago
> First of all, they kind of dodge this later by saying "Array indexing must be properly tested" (else it can panic) -- everyone thinks they write code which is "properly tested".
You're skipping half the recommendation though:
> Array indexing must be properly tested, or the get method should be used to return an Option.
emphasis mine
> Also, in rust if we don't want to panic we need to never use array indexing and never use integer division, just to start.
I mean, that's literally the block above the one you quote:
> Common patterns that can cause panics are:
> * using unwrap or expect,
> * using assert,
> * an unchecked access to an array,
> * integer overflow (in debug mode),
> * division by zero,
> * large allocations,
> * string formatting using format!.
>> Rule LANG-NOPANIC
>> Functions or instructions that can cause the code to panic at runtime must not be used.
The bit you quote is really a additional reminder that array indexing is not panic-safe.
- heavenlyblue 7y agoWhat do you do when your algorithm works with array indexing and you are never supposed to handle than None within the Option? Ie if you have indexed array out of bounds that’s a developer’s mistake and not a “condition to be handled up the stack”. It’s ok to panic if you agree that it’s better to panic rather than end up in a state that will probably cause even more issues down the line
- masklinn 7y ago> What do you do when your algorithm works with array indexing and you are never supposed to handle than None within the Option? That's where the first clause comes into play: > Array indexing must be properly tested Although > Ie if you have indexed array out of bounds that’s a developer’s mistake Which developer would be my question. If it's the person who reified the "algorithm [which] works with array indexing" then fair's fair, it's a bug in the implementation and should not happen, which is why the guideline specifically says: > should never use functions or instructions that can fail and cause the code to panic which, assuming the word is used in the RFC 2119 sense means there can be case where calling panic-ing functions is the right thing to do. If it's the developer who calls the function implementing the algorithm, then either algorithm is failable (and thus so's the function) or the algorithm should take in more specific data structures which statically exclude failing cases. For instance `max` on an empty collection will fail, so either `max` should be failable (which e.g. `Iterator::max` is) or it should only be callable on some sort of `NonEmpty*`.
- SkyMarshal 7y agoYou're skipping half the recommendation though: > Array indexing must be properly tested, or the get method should be used to return an Option. Fwiw, for anyone who’s dabbled with Haskell this is SOP. Any result that could be undefined is returned as a Maybe (Haskell’s version of Option). If this seems odd to anyone, understanding it in Haskell will probably help understand it better in Rust too: http://learnyouahaskell.com/a-fistful-of-monads#getting-our-feet-wet-with-maybe http://learnyouahaskell.com/a-fistful-of-monads#getting-our-... https://en.wikibooks.org/wiki/Haskell/Understanding_monads/Maybe https://en.wikibooks.org/wiki/Haskell/Understanding_monads/M...
- geofft 7y agoEr, don't Haskell's head and (!!) functions default to throwing an exception? $ ghci Prelude> let a = [3, 4, 5] Prelude> a !! 0 3 Prelude> a !! 3 *** Exception: Prelude.(!!): index too large Prelude> head [] *** Exception: Prelude.head: empty list I don't think Haskell is really different from Rust in this respect. Both have a wrapper type for optional values with good syntax, but in both, there's still some syntax so common operations sometimes choose to skip it. In fact, Hoogle doesn't seem to find me a function like Rust's .get() in the standard library, just a handful of third-party packages: https://hoogle.haskell.org/?hoogle=%5Ba%5D+-%3E+Int+-%3E+Maybe+a&scope=set%3Astackage https://hoogle.haskell.org/?hoogle=%5Ba%5D+-%3E+Int+-%3E+May...
- EvilTerran 7y ago> Hoogle doesn't seem to find me a function like Rust's .get() in the standard library In practice, you don't really need one - the safe alternative to "xs !! n" is pattern-matching on the result of "drop n xs", as that's [] if xs has ≤n elements: https://www.haskell.org/onlinereport/standard-prelude.html#$vdrop https://www.haskell.org/onlinereport/standard-prelude.html#$...
- masklinn 7y ago> In practice, you don't really need one - the safe alternative to "xs !! n" is pattern-matching on the result of "drop n xs", as that's [] if xs has ≤n elements: Great so instead of `xs !! n` you're supposed to write case drop n xs of [] -> … x :: _ -> … that seems… less than likely?
- CJefferson 7y agoSaying "properly tested" doesn't seem useful to me. What is "properly tested"? I'm sure people thing most array overflows in C libraries are "properly tested", as noone wants to cause memory corruption.
- masklinn 7y ago> Saying "properly tested" doesn't seem useful to me. I'm sure people thing most array overflows in C libraries are "properly tested" It is useful in the sense that it's for a restricted set of constructs, in the same sense that `unsafe` blocks are not outright forbidden but they should be justified and because they're restricted in span they can be more easily tested than your entire C codebase. Since array indexing would be recommended against by default (and either iterators or `get` would be the normal way to handle it), the number of places where it is used should be small and thus easy to check for, and test extensively if not exhaustively.
- baby 7y agoThere’s a number of functions that panic in the std if misused, for example copy_from_slice
- masklinn 7y agoYes?