以下の内容はhttps://r9.hateblo.jp/entry/2022/06/05/224856より取得しました。


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



以上の内容はhttps://r9.hateblo.jp/entry/2022/06/05/224856より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14