Skip to content

Commit

Permalink
doc: Add better explanation
Browse files Browse the repository at this point in the history
  • Loading branch information
orion GONZALEZ (contractor) committed Mar 4, 2024
1 parent 53ed660 commit cace4b0
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion compiler/rustc_index/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,19 @@ use std::vec;
use crate::{Idx, IndexSlice};

/// An owned contiguous collection of `T`s, indexed by `I` rather than by `usize`.
/// Its purpose is to avoid mixing indexes.
///
/// Why use this instead of a `Vec`?
/// This enforces the user to index a given IndexVec only with the associated index thus making it
/// impossible to use the wrong index for a given `IndexVec`.
///
/// ```compile_fail
/// use rustc_index::{Idx, IndexVec};
///
/// fn f<I1: Idx, I2: Idx>(vec1: IndexVec<I1, u8>, idx1: I1, idx2: I2) {
/// &vec1[idx1]; // Ok
/// &vec1[idx2]; // Error!
/// }
/// ```
///
/// While it's possible to use `u32` or `usize` directly for `I`,
/// you almost certainly want to use a [`newtype_index!`]-generated type instead.
Expand Down

0 comments on commit cace4b0

Please sign in to comment.