Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //https://play.rust-lang.org/?gist=c7fd2d14e8640ed21533ac27ecf1197e&version=stable
- use std::cell::RefCell;
- fn immutably_borrows(a: &i32) {
- println!("a is {}", a);
- }
- fn mutably_borrows(a: &mut i32) {
- *a +=1;
- }
- fn demo_1(mut r: &mut i32) {
- immutably_borrows(&r);
- mutably_borrows(&mut r);
- immutably_borrows(&r);
- }
- fn demo_2(r: &RefCell<i32>) {
- immutably_borrows(&r.borrow());
- mutably_borrows(&mut r.borrow_mut());
- immutably_borrows(&r.borrow());
- }
- fn main() {
- let mut data1 = 5;
- let data2 = RefCell::new(5);
- demo_1(&mut data1);
- demo_2(&data2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement