‹ BackHN Continuity

Thread

Rusty thoughts on "Parse, don't validate"

95 points · 47 comments · ingve

  1. OptionOfT · · focus · HN ↗
    On the footnote:

    > [1] Other languages - like Go or Python - have a runtime check that raises some sort of exception or panic when lst[0] is accessed on an empty list or slice.

    Rust has the same thing. Accessing a `Vec` by index goes via the index trait: <a href="https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;ops&#x2F;trait.Index.html#tymethod.index" rel="nofollow">https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;ops&#x2F;trait.Index.html#tymethod....

    Vec implements Index here: <a href="https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rust&#x2F;blob&#x2F;d080e7dff1b0fc54541545252818f8cccf995d05&#x2F;library&#x2F;alloc&#x2F;src&#x2F;vec&#x2F;mod.rs#L3962-L3971" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rust&#x2F;blob&#x2F;d080e7dff1b0fc5454154...

    Vec&#x27;s Index defers to slice&#x27;s: <a href="https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rust&#x2F;blob&#x2F;d080e7dff1b0fc54541545252818f8cccf995d05&#x2F;library&#x2F;core&#x2F;src&#x2F;slice&#x2F;index.rs#L9-L21" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rust&#x2F;blob&#x2F;d080e7dff1b0fc5454154...

    Slice defers to... intrinsics: <a href="https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rust&#x2F;blob&#x2F;d080e7dff1b0fc54541545252818f8cccf995d05&#x2F;library&#x2F;core&#x2F;src&#x2F;slice&#x2F;index.rs#L235-L239" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rust&#x2F;blob&#x2F;d080e7dff1b0fc5454154...

    Which injects a bounds check: <a href="https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rust&#x2F;blob&#x2F;d080e7dff1b0fc54541545252818f8cccf995d05&#x2F;compiler&#x2F;rustc_mir_build&#x2F;src&#x2F;builder&#x2F;expr&#x2F;as_place.rs#L647" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rust&#x2F;blob&#x2F;d080e7dff1b0fc5454154...

    The bounds check: <a href="https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rust&#x2F;blob&#x2F;d080e7dff1b0fc54541545252818f8cccf995d05&#x2F;compiler&#x2F;rustc_mir_build&#x2F;src&#x2F;builder&#x2F;expr&#x2F;as_place.rs#L727-L756" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rust&#x2F;blob&#x2F;d080e7dff1b0fc5454154...

    So I&#x27;d say the footnote is not correct.

    1. eliben · · focus · HN ↗
      You are technically correct :) But that&#x27;s not the point of the footnote, and you&#x27;re right that I should update it. The point here is that Python and Go don&#x27;t have the equivalent of &quot;first&quot;, and have the programmer rely on the runtime error in lst[0]. Rust can affort to have a &quot;first&quot; method because of Option.
Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.