Perl日記

日々の知ったことのメモなどです。Perlは最近やってないです。

Rust でブロック内に定義した関数とか構造体とかはそのスコープで閉じる

メモ。
関数とか構造体とかは外側で定義してるイメージだけど、別にどこでも定義できるらしい。
でもスコープが発生するので見えないところからは使えないらしい。

fn main() {
    {
        #[derive(Debug)]
        struct Foo;
        let foo = Foo {};
        println!("{:?}", foo);
    }
    {
        let foo = Foo {};
        println!("{:?}", foo);
    }
    {
        fn bar() {
            println!("bar");
        }
        bar();
    }
    {
        bar();
    }
}
error[E0422]: cannot find struct, variant or union type `Foo` in this scope
 --> src\main.rs:9:19
  |
9 |         let foo = Foo {};
  |                   ^^^ not found in this scope

error[E0425]: cannot find function `bar` in this scope
  --> src\main.rs:18:9
   |
18 |         bar();
   |         ^^^ not found in this scope