# Multiple mutable methods

**URL:** <https://users.rust-lang.org/t/multiple-mutable-methods/27715>\
**Category:** help\
**Created:** [April 28, 2019, 1:36pm UTC](https://users.rust-lang.org/t/multiple-mutable-methods/27715 "2019-04-28T13:36:34Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![dakom](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/dakom/32/23697_2.png) [@dakom](https://users.rust-lang.org/u/dakom)\
**Post date:** [April 28, 2019, 1:36pm UTC](https://users.rust-lang.org/t/multiple-mutable-methods/27715/1 "2019-04-28T13:36:35Z")

</div>

How do you work around something like this?

```rust
struct Foo;

impl Foo {
    fn bar(self: &mut Self) {
        self.baz();
    }
    
    fn baz (self: &mut Self) {
        
    }
}

fn main() {
    let foo = Foo{};
    
    foo.bar();
}

```

---

<div class="post-metadata">

**Author:** ![leudz](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/leudz/32/10514_2.png) [@leudz](https://users.rust-lang.org/u/leudz)\
**Post date:** [April 28, 2019, 1:42pm UTC](https://users.rust-lang.org/t/multiple-mutable-methods/27715/2 "2019-04-28T13:42:35Z")

</div>

As the compiler says, you didn't declare foo as mutable.

```rust
let mut foo = Foo{};

```

---

<div class="post-metadata">

**Author:** ![dakom](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/dakom/32/23697_2.png) [@dakom](https://users.rust-lang.org/u/dakom)\
**Post date:** [April 28, 2019, 2:12pm UTC](https://users.rust-lang.org/t/multiple-mutable-methods/27715/3 "2019-04-28T14:12:47Z")

</div>

Followup question... the following fails with

```rust
cannot borrow `*self` as mutable because it is also borrowed as immutable

```

```rust
struct Foo<'a> (&'a str);

impl<'a> Foo<'a> {
    fn init(self: &'a mut Self) {
        let value = self.get_value();
        self.do_something(value);
    }
    
    fn get_value(self: &'a Self) -> &'a str {
        self.0
    }
    
    fn do_something(self:&'a mut Self, _: &'a str) {
        
    }
}

fn main() {
    let mut foo = Foo ("hello world!");
    
    foo.init();
}

```

And if I change `get_value` to accept `self: &'a mut Self` then I get:

```rust
error[E0499]: cannot borrow `*self` as mutable more than once at a time

```

Ideas? Gotta run - but will be back later and excited to see some thoughts on this 🙂

---

<div class="post-metadata">

**Author:** ![OptimisticPeach](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/optimisticpeach/32/19544_2.png) [@OptimisticPeach](https://users.rust-lang.org/u/OptimisticPeach)\
**Post date:** [April 28, 2019, 2:56pm UTC](https://users.rust-lang.org/t/multiple-mutable-methods/27715/4 "2019-04-28T14:56:57Z")

</div>

Well, the problem here, is that `init` will first immutably bind `&mut self` until `value` is dropped, meaning that you can't run a `&mut self` function, such as `do_something`. It can't guarantee you won't modify `self` in such a way that would invalidate the borrowed reference, `&'a str`.

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/rustyyato/32/49247_2.png) [@RustyYato](https://users.rust-lang.org/u/RustyYato)\
**Post date:** [April 28, 2019, 2:58pm UTC](https://users.rust-lang.org/t/multiple-mutable-methods/27715/5 "2019-04-28T14:58:47Z")

</div>

Don't mark `&Self` or `&mut Self` with lifetimes, because they don't represent the lifetimes of the strings inside. ~~But even if you do that it won't compile because of what @OptimisticPeach said.~~  
It should compile.

---

<div class="post-metadata">

**Author:** ![dakom](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/dakom/32/23697_2.png) [@dakom](https://users.rust-lang.org/u/dakom)\
**Post date:** [April 28, 2019, 7:13pm UTC](https://users.rust-lang.org/t/multiple-mutable-methods/27715/6 "2019-04-28T19:13:58Z")

</div>

> [@RustyYato](#):
>
> Don’t mark `&Self` or `&mut Self` with lifetimes

Is that a general rule of thumb, or just specific here? I do see that removing that here fixes it...

---

<div class="post-metadata">

**Author:** ![Hyeonu](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/hyeonu/32/7970_2.png) [@Hyeonu](https://users.rust-lang.org/u/Hyeonu)\
**Post date:** [April 28, 2019, 7:46pm UTC](https://users.rust-lang.org/t/multiple-mutable-methods/27715/7 "2019-04-28T19:46:04Z")

</div>

Where did you found the code like `(self: &mut Self)`? I know it's valid syntax, but your snippet is the first code I've ever seen to use it. Afaik, everyone just write `(&self)` and `(&mut self)` instead.

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/rustyyato/32/49247_2.png) [@RustyYato](https://users.rust-lang.org/u/RustyYato)\
**Post date:** [April 28, 2019, 8:03pm UTC](https://users.rust-lang.org/t/multiple-mutable-methods/27715/8 "2019-04-28T20:03:54Z")

</div>

In this case.

This is the thought process.

```rust
struct Foo<'a> (&'a str);

impl<'a> Foo<'a> {
    fn new(string: &'a str) -> Self {
        Self(string)
    }

    fn set(&mut self, string: &'a str) {
        self.0 = string;
    }
    
    fn get(&self) -> &'a str {
        self.0
    }
 }

```

let's examine this code to show how it works. `Foo` stores a reference to a string slice of some lifetime. `Foo::new` creates a new foo with the same lifetime of the parameter. Note: it is true that `Foo<'a>: 'a` but it is not true that `&'b Foo<'a>: 'a` because `'a` could outlive `'b`. (Note: that if `'a == 'b` then `&'b Foo<'a>: 'a` is true because it boils down to `'b: 'a`, which is trivially true if `'b == 'a`)

```rust
let foo = Foo::new("hi"); // Foo<'static>
let foo_ref = &foo_ref; // ref is not static, so its lifetime is outlived by 'static

```

Because of this, we can say that `'a` does not depend on `&self`. Instead it depends on some outside source. So we shouldn't mark references to `self` with lifetimes.  
In general, don't mark lifetimes unless the compiler complains about it. Then only mark as few lifetimes as possible.

Another way to figure out which lifetimes are needed is to track how you are getting the lifetime. In my example, we get `'a` from `&'a str`, which we get from an outside source in `Foo::new`. This means that `'a` will necessarily outlive `Foo<'a>` because `'a` depends on some outside source, and that it shouldn't be related to `self` in any other way.  
The reason that `'a` will necessarily outlive `Foo<'a>` is because the outside source is now borrowed, and so can't move or be dropped. So it must live for as long as `'a` which will live until `Foo<'a>` is dropped.

* * *

It is possible to get lifetimes that refer to the struct in safe Rust. But this only really comes up in self-referential types. These types are hard to work with correctly, because of issues like yours but where you can't remove the lifetimes. So instead of dealing with the lifetimes you would use the `Pin` api along with some `unsafe`. In fact, this is exactly how `async`/`await` works!

[safe self-referential type example](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=967133dca93f6aaedf0742a78f8a0cae)

* * *

edit for clarity and correctness based off of @TomP and @Yandros 's comments later on

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/rustyyato/32/49247_2.png) [@RustyYato](https://users.rust-lang.org/u/RustyYato)\
**Post date:** [April 28, 2019, 8:06pm UTC](https://users.rust-lang.org/t/multiple-mutable-methods/27715/9 "2019-04-28T20:06:39Z")

</div>

`&self` is sugar for `self: &Self`, same with `&mut self` being sugar for `&mut Self`. `&Self` can be generalized to other types like so `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>` under the arbitrary self type RFC. So far only `&_`, `&mut _`, `Box<_>`, `Rc<_>`, `Arc<_>` are supported, but if it lands, many other types could also do this and use the method call sugar.

[https://github.com/rust-lang/rust/issues/44874](https://github.com/rust-lang/rust/issues/44874)

---

<div class="post-metadata">

**Author:** ![dakom](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/dakom/32/23697_2.png) [@dakom](https://users.rust-lang.org/u/dakom)\
**Post date:** [April 28, 2019, 8:22pm UTC](https://users.rust-lang.org/t/multiple-mutable-methods/27715/10 "2019-04-28T20:22:18Z")

</div>

Fantastic explanation... makes my brain hurt but I kindof understand it... don't think I'd be able to see it for myself when writing code yet, but at least it makes sense. Thanks!

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/rustyyato/32/49247_2.png) [@RustyYato](https://users.rust-lang.org/u/RustyYato)\
**Post date:** [April 28, 2019, 8:23pm UTC](https://users.rust-lang.org/t/multiple-mutable-methods/27715/11 "2019-04-28T20:23:38Z")

</div>

Self referential types are extremely niche so I wouldn't be surprised if they never come up. But lifetimes are worth investing your time to understand. Though that may take a while because lifetimes are hard.

---

<div class="post-metadata">

**Author:** ![Hyeonu](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/hyeonu/32/7970_2.png) [@Hyeonu](https://users.rust-lang.org/u/Hyeonu)\
**Post date:** [April 28, 2019, 8:25pm UTC](https://users.rust-lang.org/t/multiple-mutable-methods/27715/12 "2019-04-28T20:25:54Z")

</div>

Yeah I know that, but that feature is mainly for other types like Box, not for plain references. Imo `self: &Self` should not be promoted for the sake of consistency, and afaik, most Rust codes available on the web does not use such pattern. That's why I wonder where OP learned this pattern.

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/rustyyato/32/49247_2.png) [@RustyYato](https://users.rust-lang.org/u/RustyYato)\
**Post date:** [April 28, 2019, 8:27pm UTC](https://users.rust-lang.org/t/multiple-mutable-methods/27715/13 "2019-04-28T20:27:14Z")

</div>

Ok, I wasn't aware that you knew that. I agee that we should use `&self` instead of `self: &Self`

---

<div class="post-metadata">

**Author:** ![dakom](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/dakom/32/23697_2.png) [@dakom](https://users.rust-lang.org/u/dakom)\
**Post date:** [April 28, 2019, 10:06pm UTC](https://users.rust-lang.org/t/multiple-mutable-methods/27715/14 "2019-04-28T22:06:23Z")

</div>

> [@RustyYato](#):
>
> Note: it is true that `Foo<'a>: 'a` but it is not true that `&'b Foo<'a>: 'a` because `'a` could outlive `'b` .

Do you mind elaborating on this a bit more? Specifically:

1. How do you read `Foo<'a>: 'a` in English? Looks like the same syntax as Trait Inheritance...

2. So I see that `'a` comes from the input, but what is `'b`?

3. I'm also not clear on what `&'b Foo<'a>: 'a` means exactly, but I guess that will flow from the above question too...

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/rustyyato/32/49247_2.png) [@RustyYato](https://users.rust-lang.org/u/RustyYato)\
**Post date:** [April 28, 2019, 10:22pm UTC](https://users.rust-lang.org/t/multiple-mutable-methods/27715/15 "2019-04-28T22:22:41Z")

</div>

Ok

1. the syntax `Type: lifetime` means that `Type` outlives `lifetime`  
a. Similar to how `lifetime: lifetime` means that the first lifetime outlives the second lifetime
2. `'b` is the lifetime of the reference, which may or may not be the same as `'a`  
a. I did this is because there is no guarantee that these lifetimes are correlated
3. So all together `&'b Foo<'a>: 'a` means: a shared reference to `Foo<'a>` with the lifetime `'b` outlives lifetime `'a` [this is only the case of `'b == 'a`]  
a. Note that this is a false statement if `'a` [strictly] outlives `'b`, because `'a` will refer to a larger scope then `'b` (note that because of nll, the scopes that I am talking about are not lexical scopes, but are rather invisible and refer to how long certain references live)  
b. the actual statement should be `for<'b> &'b Foo<'a>: 'a` but I didn't want to get into universal quantification syntax because it is strange and I didn't want to overload information too much

* * *

edits for clarity based off of @TomP and @Yandros's comments later on

---

<div class="post-metadata">

**Author:** ![dakom](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/dakom/32/23697_2.png) [@dakom](https://users.rust-lang.org/u/dakom)\
**Post date:** [April 28, 2019, 11:08pm UTC](https://users.rust-lang.org/t/multiple-mutable-methods/27715/16 "2019-04-28T23:08:56Z")

</div>

Thanks again!!

I looked for some of this in the Rust book and now I see - it's in the _Advanced Lifetimes_ section under `Lifetime Subtyping`:

> we can declare a lifetime `'a` as usual and declare a lifetime `'b` that lives at least as long as `'a` by declaring `'b` using the syntax `'b: 'a` .

Great, will need to go back and re-read some of what you wrote above, armed with this knowledge... though I like the way you put it a bit better (not that 'b lives at least as long as 'a, but that 'b must outlive 'a... more active language, easier to grok)

The `forall 'b` addition actually helps. Not sure why, but it does something to sink it in a bit.

OK so let's see if I got this right (now that I've written it I see I'm just repeating what you said it seems... but helps to double-check): - it's basically boiling down to saying that a reference to a thing cannot live longer than the thing itself... and in this specific case - since `Foo` is created from `'a`, it's tied to `'a`, so a reference to Foo must not live longer than `'a` in order to be valid?

I guess next step would be to go and look at your self-referential example and try to understand why `fn set_ptr(&'a self) {` can't be `fn set_ptr(&self) {` ... I get you're saying that this almost never comes up but at a glance it looks _exactly_ like the kind of thing I'm trying to do for some reason, i.e. have a struct update itself but not thinking through all the implications...

Heading to bed now, hopefully the answer will appear in my dreams 💤

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/rustyyato/32/49247_2.png) [@RustyYato](https://users.rust-lang.org/u/RustyYato)\
**Post date:** [April 28, 2019, 11:51pm UTC](https://users.rust-lang.org/t/multiple-mutable-methods/27715/17 "2019-04-28T23:51:29Z")

</div>

> [@dakom](#):
>
> it’s basically boiling down to saying that a reference to a thing cannot live longer than the thing itself… and in this specific case - since `Foo` is created from `'a` , it’s tied to `'a` , so a reference to Foo must not live longer than `'a` in order to be valid?

This is correct!

> [@dakom](#):
>
> I guess next step would be to go and look at your self-referential example and try to understand why `fn set_ptr(&'a self) {` can’t be `fn set_ptr(&self) {` … I get you’re saying that this almost never comes up but at a glance it looks _exactly_ like the kind of thing I’m trying to do for some reason, i.e. have a struct update itself but not thinking through all the implications…

Ok, if you can give an example that is closer to what your actual problem is, then I or someone else here on the forums can give more insight on other was on organizing your code differently to avoid self-referential types. Self-referential types are a pain to deal with so I would really try and avoid them if you can.

---

<div class="post-metadata">

**Author:** ![TomP](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/tomp/32/4735_2.png) [@TomP](https://users.rust-lang.org/u/TomP)\
**Post date:** [April 29, 2019, 12:09am UTC](https://users.rust-lang.org/t/multiple-mutable-methods/27715/18 "2019-04-29T00:09:12Z")

</div>

> [@dakom](#):
>
> though I like the way you put it a bit better (not that 'b lives at least as long as 'a, but that 'b must outlive 'a… more active language, easier to grok)

Unfortunately the first statement's "lives **at least** as long as" was correct – `lifetime('b) ≥ lifetime('a)` – while the second's "outlive" means `lifetime('b) > lifetime('a)`. Notice the difference: the first is a reflexive inequality while the second is not. When the compiler analyzes such requirements it frequently chooses `'b = 'a`, which the second formulation excludes.

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/rustyyato/32/49247_2.png) [@RustyYato](https://users.rust-lang.org/u/RustyYato)\
**Post date:** [April 29, 2019, 1:55am UTC](https://users.rust-lang.org/t/multiple-mutable-methods/27715/19 "2019-04-29T01:55:01Z")

</div>

This is a matter of definition, if you take outlives to mean `>` then yes it is incorrect, but if you take outlives to mean `>=` then using outlives is fine. Basically saying `'a` outlives `'a` isn't fundamentally wrong. It may seem strange, but that is fine because I find that using the word "outlives" usually makes relationships more clear than "lives at least as long as".

---

<div class="post-metadata">

**Author:** ![TomP](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/tomp/32/4735_2.png) [@TomP](https://users.rust-lang.org/u/TomP)\
**Post date:** [April 29, 2019, 1:58am UTC](https://users.rust-lang.org/t/multiple-mutable-methods/27715/20 "2019-04-29T01:58:56Z")

</div>

By that logic, the statement "He `xyz` more than anyone" is acceptable, for arbitrary `xyz`, even though the statement clearly excludes the subject and thus leaves a null set.

[Next page](https://users.rust-lang.org/t/multiple-mutable-methods/27715.md?page=2)
