]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/borrow-tuple-fields.rs
Implement tuple and tuple struct indexing
[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 #![feature(tuple_indexing)]
12
13 struct Foo(int, int);
14
15 fn main() {
16     let x = (1i, 2i);
17     let a = &x.0;
18     let b = &x.0;
19     assert_eq!(*a, 1);
20     assert_eq!(*b, 1);
21
22     let mut x = (1i, 2i);
23     {
24         let a = &x.0;
25         let b = &mut x.1;
26         *b = 5;
27         assert_eq!(*a, 1);
28     }
29     assert_eq!(x.0, 1);
30     assert_eq!(x.1, 5);
31
32
33     let x = Foo(1i, 2i);
34     let a = &x.0;
35     let b = &x.0;
36     assert_eq!(*a, 1);
37     assert_eq!(*b, 1);
38
39     let mut x = Foo(1i, 2i);
40     {
41         let a = &x.0;
42         let b = &mut x.1;
43         *b = 5;
44         assert_eq!(*a, 1);
45     }
46     assert_eq!(x.0, 1);
47     assert_eq!(x.1, 5);
48 }