Skip to content

Commit cace4b0

Browse files
author
orion GONZALEZ (contractor)
committed
doc: Add better explanation
1 parent 53ed660 commit cace4b0

File tree

1 file changed

+13
-1
lines changed
  • compiler/rustc_index/src

1 file changed

+13
-1
lines changed

compiler/rustc_index/src/vec.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,19 @@ use std::vec;
1212
use crate::{Idx, IndexSlice};
1313

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

0 commit comments

Comments
 (0)