File: type-ascription.rs

package info (click to toggle)
rustc-web 1.70.0%2Bdfsg1-7~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,517,036 kB
  • sloc: xml: 147,962; javascript: 10,210; sh: 8,590; python: 8,220; ansic: 5,901; cpp: 4,635; makefile: 4,006; asm: 2,856
file content (39 lines) | stat: -rw-r--r-- 947 bytes parent folder | download | duplicates (3)
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
// run-pass

#![allow(dead_code)]
#![allow(unused_variables)]
// Type ascription doesn't lead to unsoundness

#![feature(type_ascription)]

use std::mem;

const C1: u8 = type_ascribe!(10, u8);
const C2: [u8; type_ascribe!(1, usize)] = [1];

struct S {
    a: u8
}

fn main() {
    assert_eq!(type_ascribe!(C1.into(), i32), 10);
    assert_eq!(C2[0], 1);

    let s = S { a: type_ascribe!(10, u8) };
    let arr = &[1u8, 2, 3];

    let mut v = type_ascribe!(arr.iter().cloned().collect(), Vec<_>);
    v.push(4);
    assert_eq!(v, [1, 2, 3, 4]);

    let a = type_ascribe!(1, u8);
    let b = type_ascribe!(a.into(), u16);
    assert_eq!(v[type_ascribe!(a.into(), usize)], 2);
    assert_eq!(mem::size_of_val(&a), 1);
    assert_eq!(mem::size_of_val(&b), 2);
    assert_eq!(b, type_ascribe!(1, u16));

    let mut v = Vec::new();
    type_ascribe!(v, Vec<u8>) = vec![1, 2, 3]; // Place expression type ascription
    assert_eq!(v, [1u8, 2, 3]);
}