100 exercises to learn Rust
This commit is contained in:
20
book/src/06_ticket_management/14_index_mut.md
Normal file
20
book/src/06_ticket_management/14_index_mut.md
Normal file
@@ -0,0 +1,20 @@
|
||||
# Mutable indexing
|
||||
|
||||
`Index` allows read-only access. It doesn't let you mutate the value you
|
||||
retrieved.
|
||||
|
||||
## `IndexMut`
|
||||
|
||||
If you want to allow mutability, you need to implement the `IndexMut` trait.
|
||||
|
||||
```rust
|
||||
// Slightly simplified
|
||||
pub trait IndexMut<Idx>: Index<Idx>
|
||||
{
|
||||
// Required method
|
||||
fn index_mut(&mut self, index: Idx) -> &mut Self::Output;
|
||||
}
|
||||
```
|
||||
|
||||
`IndexMut` can only be implemented if the type already implements `Index`,
|
||||
since it unlocks an _additional_ capability.
|
||||
Reference in New Issue
Block a user