File: basic.rs

package info (click to toggle)
rustc-web 1.78.0%2Bdfsg1-2~deb12u3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,245,420 kB
  • sloc: xml: 147,985; javascript: 18,022; sh: 11,083; python: 10,265; ansic: 6,172; cpp: 5,023; asm: 4,390; makefile: 4,269
file content (61 lines) | stat: -rw-r--r-- 1,402 bytes parent folder | download | duplicates (6)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//@ check-pass

// Basic usage patterns of free & associated generic const items.

#![feature(generic_const_items)]
#![allow(incomplete_features)]

fn main() {
    const NULL<T>: Option<T> = None::<T>;
    const NOTHING<T>: Option<T> = None; // arg inferred

    let _ = NOTHING::<String>;
    let _: Option<u8> = NULL; // arg inferred

    const IDENTITY<const X: u64>: u64 = X;

    const COUNT: u64 = IDENTITY::<48>;
    const AMOUNT: u64 = IDENTITY::<COUNT>;
    const NUMBER: u64 = IDENTITY::<{ AMOUNT * 2 }>;
    let _ = NUMBER;
    let _ = IDENTITY::<0>;

    let _ = match 0 {
        IDENTITY::<1> => 2,
        IDENTITY::<{ 1 + 1 }> => 4,
        _ => 0,
    };

    const CREATE<I: Inhabited>: I = I::PROOF;
    let _ = CREATE::<u64>;
    let _: u64 = CREATE; // arg inferred

    let _ = <() as Main<u64>>::MAKE::<u64>;
    let _: (u64, u64) = <()>::MAKE; // args inferred
}

pub fn usage<'any>() {
    const REGION_POLY<'a>: &'a () = &();

    let _: &'any () = REGION_POLY::<'any>;
    let _: &'any () = REGION_POLY::<'_>;
    let _: &'static () = REGION_POLY;
}

trait Main<O> {
    type Output<I>;
    const MAKE<I: Inhabited>: Self::Output<I>;
}

impl<O: Inhabited> Main<O> for () {
    type Output<I> = (O, I);
    const MAKE<I: Inhabited>: Self::Output<I> = (O::PROOF, I::PROOF);
}

trait Inhabited {
    const PROOF: Self;
}

impl Inhabited for u64 {
    const PROOF: Self = 512;
}