File: overloaded-autoderef.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (43 lines) | stat: -rw-r--r-- 1,177 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//@ run-pass
#![allow(unused_variables)]
#![allow(stable_features)]

use std::cell::RefCell;
use std::rc::Rc;

#[derive(PartialEq, Debug)]
struct Point {
    x: isize,
    y: isize
}

pub fn main() {
    let box_5: Box<_> = Box::new(5_usize);
    let point = Rc::new(Point {x: 2, y: 4});
    assert_eq!(point.x, 2);
    assert_eq!(point.y, 4);

    let i = Rc::new(RefCell::new(2));
    let i_value = *i.borrow();
    *i.borrow_mut() = 5;
    assert_eq!((i_value, *i.borrow()), (2, 5));

    let s = Rc::new("foo".to_string());
    assert_eq!(&**s, "foo");

    let mut_s = Rc::new(RefCell::new(String::from("foo")));
    mut_s.borrow_mut().push_str("bar");
    // HACK assert_eq! would panic here because it stores the LHS and RHS in two locals.
    assert_eq!(&**mut_s.borrow(), "foobar");
    assert_eq!(&**mut_s.borrow_mut(), "foobar");

    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});

    let v = Rc::new(RefCell::new([1, 2, 3]));
    v.borrow_mut()[0] = 3;
    v.borrow_mut()[1] += 3;
    assert_eq!((v.borrow()[0], v.borrow()[1], v.borrow()[2]), (3, 5, 3));
}