compiler/fight-with-compiler/borrowing/borrow-distinct-fields-of-struct #984
Replies: 5 comments
-
use std::ops::{Deref, DerefMut}; pub struct Foo {
} |
Beta Was this translation helpful? Give feedback.
-
<<对抗 Rust 编译检查系列>> 链接失效了 |
Beta Was this translation helpful? Give feedback.
-
use std::rc::Rc; pub struct Foo {
} |
Beta Was this translation helpful? Give feedback.
-
use std::cell::RefCell;
use std::rc::Rc;
pub struct Foo {
pub foo1: Vec<bool>,
pub foo2: Vec<i32>,
}
fn main() {
let foo_cell = Rc::new(RefCell::new(Foo {
foo1: vec![true, false],
foo2: vec![1, 2],
}));
let Foo { foo1, foo2 } = &mut *foo_cell.borrow_mut();
foo2.iter_mut().enumerate().for_each(|(idx, foo2)| {
if foo1[idx] {
*foo2 *= -1;
}
});
} |
Beta Was this translation helpful? Give feedback.
-
use std::rc::Rc;
use std::cell::RefCell;
pub struct Foo {
pub foo1: Vec<bool>,
pub foo2: Vec<i32>,
}
#[test]
fn test() {
let foo_cell = Rc::new(RefCell::new(Foo {
foo1: vec![true, false],
foo2: vec![1, 2],
}));
let mut borrow = foo_cell.borrow_mut();
let foo_cell_mut = &mut *borrow;
let foo1 = &foo_cell_mut.foo1;
foo_cell_mut.foo2.iter_mut().enumerate().for_each(|(idx, foo2)| {
if foo1[idx] {
*foo2 *= -1;
}
});
} |
Beta Was this translation helpful? Give feedback.
-
compiler/fight-with-compiler/borrowing/borrow-distinct-fields-of-struct
https://course.rs/compiler/fight-with-compiler/borrowing/borrow-distinct-fields-of-struct.html
Beta Was this translation helpful? Give feedback.
All reactions