Skip to content

Commit beb45df

Browse files
author
orion GONZALEZ (contractor)
committed
Apply suggestions
1 parent cace4b0 commit beb45df

File tree

1 file changed

+10
-5
lines changed
  • compiler/rustc_index/src

1 file changed

+10
-5
lines changed

compiler/rustc_index/src/vec.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,26 @@ use crate::{Idx, IndexSlice};
1313

1414
/// An owned contiguous collection of `T`s, indexed by `I` rather than by `usize`.
1515
///
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`.
16+
/// ## Why use this instead of a `Vec`?
17+
///
18+
/// An `IndexVec` allows element access only via a specific associated index type, meaning that
19+
/// trying to use the wrong index type (possibly accessing an invalid element) will fail at
20+
/// compile time.
21+
///
22+
/// It also documents what the index is indexing: in a `HashMap<usize, Something>` it's not
23+
/// immediately clear what the `usize` means, while a `HashMap<FieldIdx, Something>` makes it obvious.
1924
///
2025
/// ```compile_fail
2126
/// use rustc_index::{Idx, IndexVec};
2227
///
2328
/// fn f<I1: Idx, I2: Idx>(vec1: IndexVec<I1, u8>, idx1: I1, idx2: I2) {
2429
/// &vec1[idx1]; // Ok
25-
/// &vec1[idx2]; // Error!
30+
/// &vec1[idx2]; // Compile error!
2631
/// }
2732
/// ```
2833
///
2934
/// While it's possible to use `u32` or `usize` directly for `I`,
30-
/// you almost certainly want to use a [`newtype_index!`]-generated type instead.
35+
/// you almost certainly want to use a [`newtype_index!`] generated type instead.
3136
///
3237
/// This allows to index the IndexVec with the new index type.
3338
///

0 commit comments

Comments
 (0)