[英]Rust calling mutable self function from mutable self function
In the example below, I want to call spin
for every smol_socket_handle
which is simply an usize
.在下面的示例中,我想为每个
smol_socket_handle
调用spin
,这只是一个usize
。
pub fn spin_all(&mut self) -> u8 {
for (smol_socket_handle, _) in self.smol_sockets.iter_mut() {
self.spin(smol_socket_handle.clone());
}
0
}
pub fn spin(&mut self, smol_socket_handle: usize) -> u8 {
The problem is that I borrow self
as mutable twice.问题是我两次借用
self
作为 mutable 。 Once when spin_all
is called, and once again when we call spin
.一次是当
spin_all
被调用时,再一次是当我们调用spin
时。 I absolutely need spin
to work on a &mut self
.我绝对需要
spin
来处理&mut self
。 How can I make spin_all
call spin
lots of times for a mutable self
?我怎样才能让
spin_all
调用spin
多次为可变的self
?
UPDATE:更新:
So if I understood correctly,所以如果我理解正确的话,
for (smol_socket_handle, smol_socket) in self.smol_sockets.iter_mut() {
self.spin(smol_socket_handle.clone());
}
expands into扩展到
{
//borrows self mutably
let mut _iter = std::iter::IntoIterator::into_iter(self.smol_sockets.iter_mut());
loop {
match _iter.next() {
Some(loop_variable) => {
//tries to borrow self mutably again while it's still borrowed into `_iter`
self.spin(loop_variable.clone());
},
None => break,
}
}
}
? ?
The problem is that I borrow self as mutable twice.
问题是我两次借用 self 作为 mutable 。 Once when spin_all is called, and once again when we call spin.
一次是在调用 spin_all 时,再一次是在我们调用 spin 时。
No, that is not the problem.不,这不是问题。 The mutable
&mut self
of spin_all is reused by spin
, there is no conflict (in fact it would not work otherwise, as self.spin
would try to create a mutable borrow from an immutable borrow, which would fail). spin_all 的可变
&mut self
被spin
重用,没有冲突(实际上它不会工作,因为self.spin
会尝试从不可变借用创建可变借用,这会失败)。
The problem is that you're creating a mutable borrow for self.smol_sockets.iter_mut()
, and a separate and overlapping borrow for self.spin()
.问题是您正在为 self.smol_sockets.iter_mut() 创建一个可变借用,并为
self.spin()
self.smol_sockets.iter_mut()
创建一个单独且重叠的借用。
You should either avoid the borrow on the sockets iterable by cloning it as Aloso noted, or give spin
an index / key into the iterable so that it does the borrowing internally.您应该避免在 sockets 迭代上借用它,正如 Also 指出的那样克隆它,或者将索引/键
spin
到迭代中,以便它在内部进行借用。
Simplest answer for me was to declare the mutable borrows in the second function.对我来说最简单的答案是在第二个 function 中声明可变借用。 If
foo
calls bar
, do the mutable borrowing inside bar
instead of passing bar
a mutable reference, which doesn't work.如果
foo
调用bar
,请在bar
内部进行可变借用,而不是向bar
传递可变引用,这不起作用。
Do this:做这个:
pub fn foo(&mut self, thingid: u32) {
//stuff
self.bar(&thingid);
}
pub fn bar(&mut self, thingid: u32) {
self.things.get_mut(&thingid).unwrap();
//stuff
}
Don't do this as it causes an error ( error[E0502]: cannot borrow *self as immutable because it is also borrowed as mutable
).不要这样做,因为它会导致错误(
error[E0502]: cannot borrow *self as immutable because it is also borrowed as mutable
)。
pub fn foo(&mut self, thingid: u32) {
//stuff
let thing = self.things.get_mut(&thingid).unwrap();
self.bar(thing);
}
pub fn bar(&mut self, thing: &mut Thing) {
//stuff
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.