]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/borrow-tuple-fields.rs
Rollup merge of #45171 - rust-lang:petrochenkov-patch-2, r=steveklabnik
[rust.git] / src / test / run-pass / borrow-tuple-fields.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11
12 struct Foo(isize, isize);
13
14 fn main() {
15     let x = (1, 2);
16     let a = &x.0;
17     let b = &x.0;
18     assert_eq!(*a, 1);
19     assert_eq!(*b, 1);
20
21     let mut x = (1, 2);
22     {
23         let a = &x.0;
24         let b = &mut x.1;
25         *b = 5;
26         assert_eq!(*a, 1);
27     }
28     assert_eq!(x.0, 1);
29     assert_eq!(x.1, 5);
30
31
32     let x = Foo(1, 2);
33     let a = &x.0;
34     let b = &x.0;
35     assert_eq!(*a, 1);
36     assert_eq!(*b, 1);
37
38     let mut x = Foo(1, 2);
39     {
40         let a = &x.0;
41         let b = &mut x.1;
42         *b = 5;
43         assert_eq!(*a, 1);
44     }
45     assert_eq!(x.0, 1);
46     assert_eq!(x.1, 5);
47 }