2018-08-30 14:18:55 +02:00
|
|
|
// run-pass
|
2018-09-25 23:51:35 +02:00
|
|
|
#![allow(unused_variables)]
|
2018-08-31 15:02:01 +02:00
|
|
|
#![allow(stable_features)]
|
|
|
|
|
2014-03-06 19:24:11 +02:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(PartialEq, Debug)]
|
2014-03-06 19:24:11 +02:00
|
|
|
struct Point {
|
2015-03-25 17:06:52 -07:00
|
|
|
x: isize,
|
|
|
|
y: isize
|
2014-03-06 19:24:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() {
|
2021-08-25 02:39:40 +02:00
|
|
|
let box_5: Box<_> = Box::new(5_usize);
|
2014-03-06 19:24:11 +02:00
|
|
|
let point = Rc::new(Point {x: 2, y: 4});
|
|
|
|
assert_eq!(point.x, 2);
|
|
|
|
assert_eq!(point.y, 4);
|
|
|
|
|
2015-01-25 22:05:03 +01:00
|
|
|
let i = Rc::new(RefCell::new(2));
|
2014-03-06 19:24:11 +02:00
|
|
|
let i_value = *i.borrow();
|
|
|
|
*i.borrow_mut() = 5;
|
|
|
|
assert_eq!((i_value, *i.borrow()), (2, 5));
|
|
|
|
|
2014-05-25 03:10:11 -07:00
|
|
|
let s = Rc::new("foo".to_string());
|
2015-02-01 21:53:25 -05:00
|
|
|
assert_eq!(&**s, "foo");
|
2014-03-06 19:24:11 +02:00
|
|
|
|
2015-06-08 16:55:35 +02:00
|
|
|
let mut_s = Rc::new(RefCell::new(String::from("foo")));
|
2014-03-06 19:24:11 +02:00
|
|
|
mut_s.borrow_mut().push_str("bar");
|
2014-10-09 15:17:22 -04:00
|
|
|
// HACK assert_eq! would panic here because it stores the LHS and RHS in two locals.
|
2015-06-07 21:00:38 +03:00
|
|
|
assert_eq!(&**mut_s.borrow(), "foobar");
|
|
|
|
assert_eq!(&**mut_s.borrow_mut(), "foobar");
|
2014-03-06 19:24:11 +02:00
|
|
|
|
|
|
|
let p = Rc::new(RefCell::new(Point {x: 1, y: 2}));
|
|
|
|
p.borrow_mut().x = 3;
|
|
|
|
p.borrow_mut().y += 3;
|
|
|
|
assert_eq!(*p.borrow(), Point {x: 3, y: 5});
|
|
|
|
|
2015-01-25 22:05:03 +01:00
|
|
|
let v = Rc::new(RefCell::new([1, 2, 3]));
|
2014-03-06 19:24:11 +02:00
|
|
|
v.borrow_mut()[0] = 3;
|
|
|
|
v.borrow_mut()[1] += 3;
|
|
|
|
assert_eq!((v.borrow()[0], v.borrow()[1], v.borrow()[2]), (3, 5, 3));
|
|
|
|
}
|