I want to conditionally build different objects in rust.
The following code:
trait Fruit {}
struct Tomato {
y: f64,
}
impl Fruit for Tomato {}
trait FruitBuilder {
type Fruit: Fruit;
fn x(self, _: f64) -> Result<Self, ()>
where
Self: Sized,
{
Err(())
}
fn y(self, _: f64) -> Result<Self, ()>
where
Self: Sized,
{
Err(())
}
fn build(self) -> Result<Self::Fruit, ()>;
}
struct TomatoBuilder {
y: Option<f64>,
}
impl TomatoBuilder {
fn new() -> Self {
Self { y: None }
}
}
impl FruitBuilder for TomatoBuilder {
type Fruit = Tomato;
fn y(mut self, y: f64) -> Result<Self, ()> {
self.y = Some(y);
Ok(self)
}
fn build(self) -> Result<Self::Fruit, ()> {
let y = match self.y {
Some(y) => y,
None => {
return Err(());
}
};
Ok(Self::Fruit { y: y })
}
}
struct Apple {
x: f64,
y: f64,
}
impl Fruit for Apple {}
struct AppleBuilder {
x: Option<f64>,
y: Option<f64>,
}
impl AppleBuilder {
fn new() -> Self {
Self { x: None, y: None }
}
}
impl FruitBuilder for AppleBuilder {
type Fruit = Apple;
fn x(mut self, x: f64) -> Result<Self, ()> {
self.x = Some(x);
Ok(self)
}
fn y(mut self, y: f64) -> Result<Self, ()> {
self.y = Some(y);
Ok(self)
}
fn build(self) -> Result<Self::Fruit, ()> {
let x = match self.x {
Some(x) => x,
None => {
return Err(());
}
};
let y = match self.y {
Some(y) => y,
None => {
return Err(());
}
};
Ok(Self::Fruit { x: x, y: y })
}
}
fn main() {
let fruit_builder = match true {
true => TomatoBuilder::new(),
false => AppleBuilder::new(),
};
let _fruit = fruit_builder.build();
}
Gives the following error:
error[E0308]: `match` arms have incompatible types
--> src/main.rs:109:18
|
107 | let fruit_builder = match true {
| _________________________-
108 | | true => TomatoBuilder::new(),
| | -------------------- this is found to be of type `TomatoBuilder`
109 | | false => AppleBuilder::new(),
| | ^^^^^^^^^^^^^^^^^^^ expected struct `TomatoBuilder`, found struct `AppleBuilder`
110 | | };
| |_____- `match` arms have incompatible types
For more information about this error, try `rustc --explain E0308`.
Boxing the match arms resulted in basically the same error. Adding type annotation to fruit_builder: dyn FruitBuilder<Fruit = dyn Fruit + 'static> resulted in expected trait object 'dyn FruitBuilder', found struct 'TomatoBuilder'.
How should I go about fixing this error? Should I go back to the implementations and change them, or can I just encapsulate things in the main function somehow?
There is multiple ways to solve this:
Dynamic dispatch
That's what you were trying to do, but the dyn keyword need to be used behind a pointer, the easy solution would be to box them into a Box<dyn FruitBuilder<Fruit = dyn Fruit + 'static>>, but then you also need to box the resulting Fruit associated type, so you will need some heavy tricks to make this works that is not worth your mental health (I tried, and failed, at the cost of my sleep).
Enums
Just make an enum that can be either one of your builders, no dynamic dispatch, no head scratch, just lot of code:
enum Builder {
Tomato(TomatoBuilder),
Apple(AppleBuilder)
}
enum Fruits {
Tomato(Tomato),
Apple(Apple)
}
impl Fruit for Fruits {}
impl FruitBuilder for Builder {
type Fruit = Fruits;
fn x(self, x: f64) -> Result<Self, ()>
where
Self: Sized,
{
match self {
Builder::Apple(apple_builder) => {
apple_builder.x(x).map(Builder::Apple)
},
Builder::Tomato(tomato_builder) => {
tomato_builder.x(x).map(Builder::Tomato)
}
}
}
fn y(self, y: f64) -> Result<Self, ()>
where
Self: Sized,
{
match self {
Builder::Apple(apple_builder) => {
apple_builder.y(y).map(Builder::Apple)
},
Builder::Tomato(tomato_builder) => {
tomato_builder.y(y).map(Builder::Tomato)
}
}
}
fn build(self) -> Result<Self::Fruit, ()> {
match self {
Builder::Apple(apple_builder) => {
apple_builder.build().map(Fruits::Apple)
},
Builder::Tomato(tomato_builder) => {
tomato_builder.build().map(Fruits::Tomato)
}
}
}
}
fn main() {
let fruit_builder = match true {
true => Builder::Tomato(TomatoBuilder::new()),
false => Builder::Apple(AppleBuilder::new()),
};
let _fruit = fruit_builder.build();
}
yeah it's a bit too much verbose for just a wrapper, but you get the point.
Still dynamic dispatch, but much simpler
Just build your fruits in the match, then return a Box<dyn Fruit>:
fn main() {
let _fruit: Result<Box<dyn Fruit>, ()> = match true {
true => {
let builder = TomatoBuilder::new();
builder.build().map(|fruit| Box::new(fruit) as Box<dyn Fruit>)
},
false => {
let builder = AppleBuilder::new();
builder.build().map(|fruit| Box::new(fruit) as Box<dyn Fruit>)
},
};
}
Choose the solution that suits the more your needs, but if you are hable to make the enums work for what you are doing, then it would be better in my opinion. Yes it is more verbose, but no dynamic dispatch and no allocation, and probably way more opportunity for the compiler to optimize it.
Related
I am implementing my own blocking pub/sub pattern as seen below.
use std::any::{Any, TypeId};
#[derive(Clone)]
struct ModelA {
pub id: u32
}
#[derive(Clone)]
struct ModelB {
pub id: u32
}
struct ModelASubscriberOne<'a> {
pub model: &'a ModelA
}
impl<'a> Subscriber<u32> for ModelASubscriberOne<'a> {
fn get_type(&self) -> TypeId {
TypeId::of::<ModelASubscriberOne>()
}
fn execute(&self) {
println!("SubscriberOne ModelA id: {}", self.model.id);
}
fn id(&self) -> u32 {
self.model.id
}
}
struct ModelASubscriberTwo<'a> {
pub model: &'a ModelA
}
impl<'a> Subscriber<u32> for ModelASubscriberTwo<'a> {
fn get_type(&self) -> TypeId {
TypeId::of::<ModelASubscriberTwo>()
}
fn execute(&self) {
println!("SubscriberTwo ModelA id: {}", self.model.id);
}
fn id(&self) -> u32 {
self.model.id
}
}
trait Subscriber<TKey:Eq + 'static> {
fn get_type(&self) -> TypeId;
fn execute(&self);
fn id(&self) -> TKey;
fn filter(&self, other: TKey) -> bool{
self.id() == other
}
}
struct Publisher<'a> {
pub subscribers: Vec<&'a dyn Subscriber<u32>>
}
impl<'a> Publisher<'a> {
fn new() -> Publisher<'a> {
Publisher{subscribers: Vec::new()}
}
fn subscribe(&mut self, subscriber: &'a dyn Subscriber<u32>) {
self.subscribers.push(subscriber);
}
fn publish<T>(&self) where T: 'static + Subscriber<u32> {
self.subscribers.iter().filter(|x| {
TypeId::of::<T>() == x.get_type()
})
.for_each(|x| {
x.execute();
});
}
fn unsubscribe<T:Any>(&mut self, subscriber: u32) {
let position = self.subscribers.iter().position(|x| {
TypeId::of::<T>() == x.get_type() && x.filter(subscriber)
});
match position {
Some(position) => {
self.subscribers.remove(position);
},
_ => {}
}
}
}
fn main() {
let model_a = ModelA{id:0};
let mut model_a_list: Vec<ModelA> = Vec::new();
model_a_list.push(model_a);
let model_a = ModelA{id:1};
model_a_list.push(model_a);
let mut publisher = Publisher::new();
let subscriber = ModelASubscriberOne{model: &model_a_list[0]};
publisher.subscribe(&subscriber);
let subscriber = ModelASubscriberOne{model: &model_a_list[1]};
publisher.subscribe(&subscriber);
let subscriber = ModelASubscriberTwo{model: &model_a_list[1]};
publisher.subscribe(&subscriber);
println!("Subscribed");
publisher.publish::<ModelASubscriberOne>();
publisher.publish::<ModelASubscriberTwo>();
publisher.unsubscribe::<ModelASubscriberOne>(model_a_list[1].id);
println!("Unsubscribed");
publisher.publish::<ModelASubscriberOne>();
publisher.publish::<ModelASubscriberTwo>();
}
I feel like it would be helpful to be able to have a default implementation of get_type() for Subscriber that returns a TypeId for Self. Is there a way to do this?
I tried the following:
trait Subscriber<TKey:Eq + 'static> {
fn get_type(self: &Self) -> TypeId {
TypeId::of::<Self>()
}
// <...>
}
I get the following message:
error[E0310]: the parameter type `Self` may not live long enough
--> src\main.rs:51:5
|
51 | TypeId::of::<Self>()
| ^^^^^^^^^^^^^^^^^^
|
= help: consider adding an explicit lifetime bound `Self: 'static`...
= note: ...so that the type `Self` will meet its required lifetime bounds
I am not sure where to put the lifetime bound. I expect I need an Any trait bound as well but I am not sure where to put that either.
Thanks in advance.
I would like to have the following functionality
trait Policy {
fn eval(&self, k: u32) -> bool;
fn default() -> Box<dyn Policy>
where
Self: Sized,
{
Box::new(MaxPolicy { max: 2 })
}
}
struct MaxPolicy {
max: u32,
}
impl Policy for MaxPolicy {
fn eval(&self, k: u32) -> bool {
println!("MaxPolicy");
k < self.max
}
}
#[test]
fn max_policy() {
let p = MaxPolicy { max: 2 };
assert!(!p.eval(3));
}
#[test]
fn default_policy() {
let p = Policy::default();
assert!(!p.eval(3));
}
(Playground)
This does not compile:
error[E0283]: type annotations needed
--> src/lib.rs:31:13
|
4 | fn default() -> Box<dyn Policy>
| -------
5 | where
6 | Self: Sized,
| ----- required by this bound in `Policy::default`
...
31 | let p = Policy::default();
| ^^^^^^^^^^^^^^^ cannot infer type
|
= note: cannot resolve `_: Policy`
Would it possible to alter the approach to make it work? Is this even possible for trait objects to have a method returning some implementation of Self? If not, why not?
Implement default on the trait object type, not on the trait:
trait Policy {
fn eval(&self, k: u32) -> bool;
}
impl dyn Policy {
fn default() -> Box<Self> {
Box::new(MaxPolicy { max: 2 })
}
}
See also:
Why would I implement methods on a trait instead of as part of the trait?
I constructed a workaround which has a similar nice API
trait Policy {
fn eval(&self, k: u32) -> bool;
}
struct MaxPolicy {
max: u32,
}
impl Policy for MaxPolicy {
fn eval(&self, k: u32) -> bool {
println!("MaxPolicy");
k < self.max
}
}
struct MinPolicy {
min: u32,
}
impl Policy for MinPolicy {
fn eval(&self, k: u32) -> bool {
println!("MinPolicy");
k > self.min
}
}
enum PolicyEnum {
Custom(Box<dyn Policy>),
Default,
}
impl PolicyEnum {
const DEFAULT: MaxPolicy = MaxPolicy { max: 4 };
}
impl Policy for PolicyEnum {
fn eval(&self, k: u32) -> bool {
match self {
PolicyEnum::Custom(p) => p.eval(k),
PolicyEnum::Default => Self::DEFAULT.eval(k),
}
}
}
Playground
With that, it's possible to do:
#[test]
fn default() {
let p = PolicyEnum::Default;
assert!(p.eval(3));
}
#[test]
fn custom() {
let p = PolicyEnum::Custom(Box::new(MinPolicy{min: 4}));
assert!(p.eval(5));
}
I can understand borrowing/ownership concepts in Rust, but I have no idea how to work around this case:
use std::collections::{HashMap, HashSet};
struct Val {
t: HashMap<u16, u16>,
l: HashSet<u16>,
}
impl Val {
fn new() -> Val {
Val {
t: HashMap::new(),
l: HashSet::new(),
}
}
fn set(&mut self, k: u16, v: u16) {
self.t.insert(k, v);
self.l.insert(v);
}
fn remove(&mut self, v: &u16) -> bool {
self.l.remove(v)
}
fn do_work(&mut self, v: u16) -> bool {
match self.t.get(&v) {
None => false,
Some(r) => self.remove(r),
}
}
}
fn main() {
let mut v = Val::new();
v.set(123, 100);
v.set(100, 1234);
println!("Size before: {}", v.l.len());
println!("Work: {}", v.do_work(123));
println!("Size after: {}", v.l.len());
}
playground
The compiler has the error:
error[E0502]: cannot borrow `*self` as mutable because it is also borrowed as immutable
--> src/main.rs:28:24
|
26 | match self.t.get(&v) {
| ------ immutable borrow occurs here
27 | None => false,
28 | Some(r) => self.remove(r),
| ^^^^^------^^^
| | |
| | immutable borrow later used by call
| mutable borrow occurs here
I don't understand why I can't mutate in the match arm when I did a get (read value) before; the self.t.get is finished when the mutation via remove begins.
Is this due to scope of the result (Option<&u16>) returned by the get? It's true that the lifetime of the result has a scope inside the match expression, but this design-pattern is used very often (mutate in a match expression).
How do I work around the error?
The declaration of function HashMap::<K,V>::get() is, a bit simplified:
pub fn get<'s>(&'s self, k: &K) -> Option<&'s V>
This means that it returns an optional reference to the contained value, not the value itself. Since the returned reference points to a value inside the map, it actually borrows the map, that is, you cannot mutate the map while this reference exists. This restriction is there to protect you, what would happen if you remove this value while the reference is still alive?
So when you write:
match self.t.get(&v) {
None => false,
//r: &u16
Some(r) => self.remove(r)
}
the captured r is of type &u16 and its lifetime is that of self.t, that is, it is borrowing it. Thus you cannot get a mutable reference to self, that is needed to call remove.
The simplest solution for your problem is the clone() solves every lifetime issue pattern. Since your values are of type u16, that is Copy, it is actually trivial:
match self.t.get(&v) {
None => false,
//r: u16
Some(&r) => self.remove(&r)
}
Now r is actually of type u16 so it borrows nothing and you can mutate self at will.
If your key/value types weren't Copy you could try and clone them, if you are willing to pay for that. If not, there is still another option as your remove() function does not modify the HashMap but an unrelated HashSet. You can still mutate that set if you take care not to reborrow self:
fn remove2(v: &u16, l: &mut HashSet<u16>) -> bool {
l.remove(v)
}
fn do_work(&mut self, v: u16) -> bool {
match self.t.get(&v) {
None => false,
//selt.t is borrowed, now we mut-borrow self.l, no problem
Some(r) => Self::remove2(r, &mut self.l)
}
}
You are trying to remove value from HashMap by using value you get, not key.
Only line 26 is changed Some(_) => self.remove(&v)
This will work:
use std::collections::HashMap;
struct Val {
t: HashMap<u16, u16>
}
impl Val {
fn new() -> Val {
Val { t: HashMap::new() }
}
fn set(&mut self, k: u16, v: u16) {
self.t.insert(k, v);
}
fn remove(&mut self, v: &u16) -> bool {
match self.t.remove(v) {
None => false,
_ => true,
}
}
fn do_work(&mut self, v: u16) -> bool {
match self.t.get(&v) {
None => false,
Some(_) => self.remove(&v)
}
}
}
fn main() {
let mut v = Val::new();
v.set(123, 100);
v.set(1100, 1234);
println!("Size before: {}", v.t.len());
println!("Work: {}", v.do_work(123));
println!("Size after: {}", v.t.len());
}
play.rust
It seems that the following solution is good for primitive types like here u16. For other types, the ownership is moved.
use std::collections::HashMap;
struct Val {
t: HashMap<u16, u16>,
}
impl Val {
fn new() -> Val {
Val { t: HashMap::new() }
}
fn set(&mut self, k: u16, v: u16) {
self.t.insert(k, v);
}
fn remove(&mut self, v: &u16) -> bool {
match self.t.remove(v) {
None => false,
_ => true,
}
}
fn do_work(&mut self, v: u16) -> bool {
match self.t.get(&v) {
None => false,
Some(&v) => self.remove(&v)
}
}
}
fn main() {
let mut v = Val::new();
v.set(123, 100);
v.set(100, 1234);
println!("Size before: {}", v.t.len());
println!("Work: {}", v.do_work(123));
println!("Size after: {}", v.t.len());
}
For other types, we must clone the value:
use std::collections::{HashMap, HashSet};
#[derive(Debug)]
struct Val {
t: HashMap<String, String>,
l: HashSet<String>
}
impl Val {
fn new() -> Val {
Val { t: HashMap::new(), l: HashSet::new() }
}
fn set(&mut self, k: String, v: String) {
self.l.insert(v.clone());
self.t.insert(k, v);
}
fn remove(&mut self, v: &String) -> bool {
self.l.remove(v)
}
fn do_work(&mut self, i: &String) -> bool {
match self.t.get(i) {
None => false,
Some(v) => {
let x = v.clone();
self.remove(&x)
}
}
}
fn do_task(&mut self, i: &String) -> bool {
match self.t.get(i) {
None => false,
Some(v) => self.l.insert(v.clone())
}
}
}
fn main() {
let mut v = Val::new();
v.set("AA".to_string(), "BB".to_string());
v.set("BB".to_string(), "CC".to_string());
println!("Start: {:#?}", v);
println!("Size before: {}", v.l.len());
println!("Work: {}", v.do_work(&"AA".to_string()));
println!("Size after: {}", v.l.len());
println!("After: {:#?}", v);
println!("Task [Exist]: {}", v.do_task(&"BB".to_string()));
println!("Task [New]: {}", v.do_task(&"AA".to_string()));
println!("End: {:#?}", v);
}
But i'd like a solution that has no allocation
I have a data structure like this:
struct R {
hmhs: HashMap<i64, HashSet<i64>>,
}
impl R {
fn hs_for_hmhs(&mut self) -> &mut HashSet<i64> {
if let None = self.hmhs.get(&0) {
self.hmhs.insert(0, HashSet::new());
}
self.hmhs.get_mut(&0).unwrap()
}
fn iter_for_hmhs<'a>(&'a mut self) -> impl Iterator<Item = &'a i64> {
self.hs_for_hmhs().iter()
}
fn insert_for_hmhs(&mut self, i: i64) -> bool {
self.hs_for_hmhs().insert(i)
}
}
This seems to work, but all the methods require a mutable
reference to self which is unfortunate. I tried to give
interior mutability a go:
struct S {
hmhs: RefCell<HashMap<i64, HashSet<i64>>>,
}
impl S {
fn hs_for_hmhs(&self) -> &HashSet<i64> {
if let None = self.hmhs.borrow().get(&0) {
self.hmhs.borrow_mut().insert(0, HashSet::new());
}
self.hmhs.borrow_mut().get_mut(&0).unwrap()
}
fn iter_for_hmhs(&mut self) -> impl Iterator<Item = &i64> {
self.hs_for_hmhs().iter()
}
fn insert_for_hmhs(&mut self, i: i64) -> bool {
self.hs_for_hmhs().insert(i)
}
}
However, I constantly seem to hit problems. Mostly some variety of How do I return a reference to something inside a RefCell without breaking encapsulation?
I have tried lots of variants here, but I am missing something
fundamental in my understanding. Is there a way of achieving what I
want?
Complete Code:
use std::cell::RefCell;
use std::collections::{HashMap, HashSet};
struct R {
hmhs: HashMap<i64, HashSet<i64>>,
}
impl R {
fn hs_for_hmhs(&mut self) -> &mut HashSet<i64> {
if let None = self.hmhs.get(&0) {
self.hmhs.insert(0, HashSet::new());
}
self.hmhs.get_mut(&0).unwrap()
}
fn iter_for_hmhs<'a>(&'a mut self) -> impl Iterator<Item = &'a i64> {
self.hs_for_hmhs().iter()
}
fn insert_for_hmhs(&mut self, i: i64) -> bool {
self.hs_for_hmhs().insert(i)
}
}
struct S {
hmhs: RefCell<HashMap<i64, HashSet<i64>>>,
}
impl S {
fn hs_for_hmhs(&self) -> &mut HashSet<i64> {
if let None = self.hmhs.borrow().get(&0) {
self.hmhs.borrow_mut().insert(0, HashSet::new());
}
self.hmhs.borrow_mut().get_mut(&0).unwrap()
}
fn iter_for_hmhs(&self) -> impl Iterator<Item = &i64> {
self.hs_for_hmhs().iter()
}
fn insert_for_hmhs(&self, i: i64) -> bool {
self.hs_for_hmhs().insert(i)
}
}
fn main() {}
Compiler Message:
error[E0597]: borrowed value does not live long enough
--> src/main.rs:36:9
|
36 | self.hmhs.borrow_mut().get_mut(&0).unwrap()
| ^^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
37 | }
| - temporary value only lives until here
|
note: borrowed value must be valid for the anonymous lifetime #1 defined on the method body at 31:5...
--> src/main.rs:31:5
|
31 | / fn hs_for_hmhs(&self) -> &mut HashSet<i64> {
32 | | if let None = self.hmhs.borrow().get(&0) {
33 | | self.hmhs.borrow_mut().insert(0, HashSet::new());
34 | | }
35 | |
36 | | self.hmhs.borrow_mut().get_mut(&0).unwrap()
37 | | }
| |_____^
I found a solution -- extract the HashMap as a raw pointer. This in turn means that I can get to the HashSet without shenanigans including returning a iterator.
I'm happy enough with this as a solution. The unsafe code is small and contained and if I understand the reason why the compiler is complaining without unsafe, it cannot occur in this code, since neither the HashMap nor the HashSet are ever removed or replaced after construction.
That was a lot of effort.
use std::cell::RefCell;
use std::collections::{HashMap, HashSet};
struct R {
hmhs: HashMap<i64, HashSet<i64>>,
}
impl R {
fn hs_for_hmhs(&mut self) -> &mut HashSet<i64> {
if let None = self.hmhs.get(&0) {
self.hmhs.insert(0, HashSet::new());
}
self.hmhs.get_mut(&0).unwrap()
}
fn iter_for_hmhs<'a>(&'a mut self) -> impl Iterator<Item = &'a i64> {
self.hs_for_hmhs().iter()
}
fn insert_for_hmhs(&mut self, i: i64) -> bool {
self.hs_for_hmhs().insert(i)
}
}
struct S {
hmhs: RefCell<HashMap<i64, HashSet<i64>>>,
}
impl S {
fn hs_as_ptr(&self) -> *mut HashMap<i64, HashSet<i64>> {
self.hmhs.borrow_mut().entry(0).or_insert(HashSet::new());
self.hmhs.as_ptr()
}
fn mut_hs_for_hmhs(&mut self) -> &mut HashSet<i64> {
unsafe { (*self.hs_as_ptr()).get_mut(&0).unwrap() }
}
fn hs_for_hmhs(&self) -> &HashSet<i64> {
unsafe { (*self.hs_as_ptr()).get(&0).unwrap() }
}
fn iter_for_hmhs<'a>(&'a self) -> impl Iterator<Item = &'a i64> + 'a {
self.hs_for_hmhs().iter()
}
fn insert_for_hmhs(&mut self, i: i64) -> bool {
self.mut_hs_for_hmhs().insert(i)
}
}
fn main() {
let mut r = R {
hmhs: HashMap::new(),
};
let mut s = S {
hmhs: RefCell::new(HashMap::new()),
};
r.insert_for_hmhs(10);
s.insert_for_hmhs(20);
println!("r next: {:?}", r.iter_for_hmhs().next());
println!("s next: {:?}", s.iter_for_hmhs().next());
}
https://play.rust-lang.org/?gist=3ed1977bdd5f9f82d144fe128f618979&version=stable&mode=debug&edition=2015
Is there a way to perform an index access to an instance of a struct like this:
struct MyStruct {
// ...
}
impl MyStruct {
// ...
}
fn main() {
let s = MyStruct::new();
s["something"] = 533; // This is what I need
}
You can use the Index and IndexMut traits.
use std::ops::{Index, IndexMut};
struct Foo {
x: i32,
y: i32,
}
impl Index<&'_ str> for Foo {
type Output = i32;
fn index(&self, s: &str) -> &i32 {
match s {
"x" => &self.x,
"y" => &self.y,
_ => panic!("unknown field: {}", s),
}
}
}
impl IndexMut<&'_ str> for Foo {
fn index_mut(&mut self, s: &str) -> &mut i32 {
match s {
"x" => &mut self.x,
"y" => &mut self.y,
_ => panic!("unknown field: {}", s),
}
}
}
fn main() {
let mut foo = Foo { x: 0, y: 0 };
foo["y"] += 2;
println!("x: {}", foo["x"]);
println!("y: {}", foo["y"]);
}
It prints:
x: 0
y: 2
You want to use the Index trait (and its pair IndexMut):
use std::ops::Index;
#[derive(Copy, Clone)]
struct Foo;
struct Bar;
impl Index<Bar> for Foo {
type Output = Foo;
fn index<'a>(&'a self, _index: Bar) -> &'a Foo {
println!("Indexing!");
self
}
}
fn main() {
Foo[Bar];
}