2

I want to implement a Rust struct that provides the [index]= operation.

For example

foo[bar] = zot

What trait does foo need to implement here? Is it perhaps some combination of std::ops?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
opus111
  • 2,744
  • 4
  • 25
  • 41
  • 1
    I believe you are looking for [Index](https://doc.rust-lang.org/std/ops/trait.Index.html) or [IndexMut](https://doc.rust-lang.org/std/ops/trait.IndexMut.html) – squiguy May 16 '18 at 20:24
  • @squiguy Going to make that an answer? – trent May 16 '18 at 20:40
  • I believe your question is answered by the answers of [Is there a way to perform an index access to an instance of a struct?](https://stackoverflow.com/q/28126735/155423). If you disagree, please [edit] your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster May 16 '18 at 20:42
  • Hi. I implemented both of those, but it didn't let me do []=. I am just learning Rust, so probably I am missing an important concept here. IndexMut certainly *sounds* like what I need... – opus111 May 16 '18 at 20:43
  • @user1902291 Please [edit] your question with more prose to describe what you expect `foo[bar] = zot` to *do*, as well as any attempts you've made, what you expect them to do, what they do, etc. – Shepmaster May 16 '18 at 20:45
  • 1
    Hi @Shepmaster. Yes I believe https://stackoverflow.com/questions/28126735/is-there-a-way-to-perform-an-index-access-to-an-instance-of-a-struct asks the same question. Thanks – opus111 May 16 '18 at 20:46
  • @user1902291 so you'd be comfortable marking this as a duplicate? – Shepmaster May 16 '18 at 20:47
  • Sure. Thank you – opus111 May 17 '18 at 00:52

0 Answers0