# Help on invalid use of mutating method on rvalue

**URL:** <https://forum.modular.com/t/help-on-invalid-use-of-mutating-method-on-rvalue/1274>\
**Category:** Standard Library\
**Tags:** mojo-compiler\
**Created:** [April 22, 2025, 2:33am UTC](https://forum.modular.com/t/help-on-invalid-use-of-mutating-method-on-rvalue/1274 "2025-04-22T02:33:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![letpat](https://avatars.discourse-cdn.com/v4/letter/l/6bbea6/32.png) [@letpat](https://forum.modular.com/u/letpat)\
**Post date:** [April 22, 2025, 2:33am UTC](https://forum.modular.com/t/help-on-invalid-use-of-mutating-method-on-rvalue/1274/1 "2025-04-22T02:33:35Z")

</div>

## environment

- magic 07.2 h
- mojo 25.2.0

I have a simple Client struct. I need to store instances of Client in a dictionary.  
But when I try to call a set method (set\_var) which changes data, compiler reports error :  
invalid call to ‘set\_var’: invalid use of mutating method on rvalue of type ‘Client’

if I use a List in place of dictionary, there is no error.

How can I set mutable to be able to call setter ?

Here is a sample code :

## sample.mojo

```auto
from collections import Dict
from collections import List

@value
struct Client[]():
    var var01: Int

    fn __init__ (out self, var01: Int):
        self.var01 = var01
    
    def set_var(mut self, var01: Int):
        self.var01 = var01
    
    def get_var(self) -> Int:
        return self.var01

def main():
    var l01 = List[Client]()

    l01.append(Client(1))
    l01[0].set_var(2)
    
    print(l01[0].get_var()) # Ok print 2

    var d01 = Dict[String, List[Client]]()
    d01['ClientA'] = List[Client]()
    d01['ClientA'].append(Client(3))
    d01['ClientA'][0].set_var(4) # error invalid use of mutating method on rvalue

```

---

<div class="post-metadata">

**Author:** ![duck\_tape](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/duck_tape/32/432_2.png) [@duck\_tape](https://forum.modular.com/u/duck_tape)\
**Post date:** [April 22, 2025, 1:28pm UTC](https://forum.modular.com/t/help-on-invalid-use-of-mutating-method-on-rvalue/1274/2 "2025-04-22T13:28:59Z")

</div>

> [@letpat](#):
>
> n I try to call a set method (set\_var) which changes data, compiler reports e

I’m not sure exactly what is the cause here, hopefully someone else can elaborate on that.

I think that one of `Dict. __getitem__ ` or `List. __getitem__ ` is actually creating a copy of the `Client`, which is the rvalue that the the message refers to. Since the rvalue isn’t bound to anything, mutating it doesn’t do anything, and that’s an error (I think my terminology here is a bit messy).

To fix it you just need to get a pointer to the dictionary value, for example:  
`d01.get_ptr("ClientA").value()[][0].set_var(4)` fixes it.

---

<div class="post-metadata">

**Author:** ![letpat](https://avatars.discourse-cdn.com/v4/letter/l/6bbea6/32.png) [@letpat](https://forum.modular.com/u/letpat)\
**Post date:** [April 22, 2025, 2:19pm UTC](https://forum.modular.com/t/help-on-invalid-use-of-mutating-method-on-rvalue/1274/3 "2025-04-22T14:19:43Z")

</div>

Thanks for yours explanation and solution.
