You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When you call a method with a borrowed self, the compiler appears to assume that the borrow of self starts before the other parameters to that method have been evaluated, which seems wrong. This forces you to create local variables unnecessarily. Here's an example of code that I think should compile, but doesn't:
structThingy{i:int}implThingy{fndo_something_mutable(&mutself,x:int){// Just assume there's a real reason for self to be mutable here...println(format!("{}", x));}fnget_i(&self) -> int{self.i}}fnmain(){letmut o = Thingy{i:2};
o.do_something_mutable(o.get_i());// This line should compile, but doesn't}
the error is this:
lifetime-call-param.rs:13:27: 13:28 error: cannot borrow `o` as immutable because it is also borrowed as mutable
lifetime-call-param.rs:13 o.do_something_mutable(o.get_i());
^
lifetime-call-param.rs:13:4: 13:5 note: second borrow of `o` occurs here
lifetime-call-param.rs:13 o.do_something_mutable(o.get_i());
^
If you replace the problematic line with this it compiles and works fine:
let y = o.get_i();
o.do_something_mutable(y);
but isn't that equivalent to the non-compiling version?
The text was updated successfully, but these errors were encountered:
You are correct that this is a bug (in fact, a duplicate of #6268).
Moving the inner call out into a temporary is the recommended fix.
(Random comment: using println!("{}", x) will be marginally higher performance than println(format!(...)) (and it looks neater), since the former can effectively write straight to stdout, while the latter has to write to an allocated string and then write the string to stdout.)
When you call a method with a borrowed
self
, the compiler appears to assume that the borrow ofself
starts before the other parameters to that method have been evaluated, which seems wrong. This forces you to create local variables unnecessarily. Here's an example of code that I think should compile, but doesn't:the error is this:
If you replace the problematic line with this it compiles and works fine:
but isn't that equivalent to the non-compiling version?
The text was updated successfully, but these errors were encountered: