> [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://doc.rust-lang.org/std/ops/trait.Index.html#tymethod.index" rel="nofollow">https://doc.rust-lang.org/std/ops/trait.Index.html#tymethod....
Vec implements Index here: <a href="https://github.com/rust-lang/rust/blob/d080e7dff1b0fc54541545252818f8cccf995d05/library/alloc/src/vec/mod.rs#L3962-L3971" rel="nofollow">https://github.com/rust-lang/rust/blob/d080e7dff1b0fc5454154...
Vec's Index defers to slice's: <a href="https://github.com/rust-lang/rust/blob/d080e7dff1b0fc54541545252818f8cccf995d05/library/core/src/slice/index.rs#L9-L21" rel="nofollow">https://github.com/rust-lang/rust/blob/d080e7dff1b0fc5454154...
Which injects a bounds check: <a href="https://github.com/rust-lang/rust/blob/d080e7dff1b0fc54541545252818f8cccf995d05/compiler/rustc_mir_build/src/builder/expr/as_place.rs#L647" rel="nofollow">https://github.com/rust-lang/rust/blob/d080e7dff1b0fc5454154...
The bounds check: <a href="https://github.com/rust-lang/rust/blob/d080e7dff1b0fc54541545252818f8cccf995d05/compiler/rustc_mir_build/src/builder/expr/as_place.rs#L727-L756" rel="nofollow">https://github.com/rust-lang/rust/blob/d080e7dff1b0fc5454154...
You are technically correct :) But that's not the point of the footnote, and you're right that I should update it. The point here is that Python and Go don't have the equivalent of "first", and have the programmer rely on the runtime error in lst[0]. Rust can affort to have a "first" method because of Option.
OptionOfT · · focus · HN ↗
> [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://doc.rust-lang.org/std/ops/trait.Index.html#tymethod.index" rel="nofollow">https://doc.rust-lang.org/std/ops/trait.Index.html#tymethod....
Vec implements Index here: <a href="https://github.com/rust-lang/rust/blob/d080e7dff1b0fc54541545252818f8cccf995d05/library/alloc/src/vec/mod.rs#L3962-L3971" rel="nofollow">https://github.com/rust-lang/rust/blob/d080e7dff1b0fc5454154...
Vec's Index defers to slice's: <a href="https://github.com/rust-lang/rust/blob/d080e7dff1b0fc54541545252818f8cccf995d05/library/core/src/slice/index.rs#L9-L21" rel="nofollow">https://github.com/rust-lang/rust/blob/d080e7dff1b0fc5454154...
Slice defers to... intrinsics: <a href="https://github.com/rust-lang/rust/blob/d080e7dff1b0fc54541545252818f8cccf995d05/library/core/src/slice/index.rs#L235-L239" rel="nofollow">https://github.com/rust-lang/rust/blob/d080e7dff1b0fc5454154...
Which injects a bounds check: <a href="https://github.com/rust-lang/rust/blob/d080e7dff1b0fc54541545252818f8cccf995d05/compiler/rustc_mir_build/src/builder/expr/as_place.rs#L647" rel="nofollow">https://github.com/rust-lang/rust/blob/d080e7dff1b0fc5454154...
The bounds check: <a href="https://github.com/rust-lang/rust/blob/d080e7dff1b0fc54541545252818f8cccf995d05/compiler/rustc_mir_build/src/builder/expr/as_place.rs#L727-L756" rel="nofollow">https://github.com/rust-lang/rust/blob/d080e7dff1b0fc5454154...
So I'd say the footnote is not correct.
eliben · · focus · HN ↗