0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

今年の言語はRust その14

Posted at

今年の言語はRust その14

Rustを学びます

Rustの日本語ドキュメント 2nd Edition
https://doc.rust-jp.rs/book/second-edition/

オリジナル(英語)
https://doc.rust-lang.org/book/

7. モジュール

7.3 use

pub mod a {
    pub mod series {
        pub mod of {
            pub fn nested_modules() {}
        }
    }
}

fn main() {
    a::series::of::nested_modules();
}

useを使うことで指定のモジュールをバイナリクレートのルクートスコープにもってこれる


pub mod a {
    pub mod series {
        pub mod of {
            pub fn nested_modules() {}
        }
    }
}

use a::series::of;

fn main() {
    of::nested_modules();
}

関数名を指定することもできる

pub mod a {
    pub mod series {
        pub mod of {
            pub fn nested_modules() {}
        }
    }
}

use a::series::of::nested_modules;

fn main() {
    nested_modules();
}

enumも名前空間がある

enum TrafficLight{
    Red,
    Yellow,
    Green,
}

use TrafficLight::{Red, Yellow};

fn main() {
    
    let red = Red;
    let yel = Yellow;
    let grn = TrafficLight::Green;
    
}

もちろん*で一括指定もできます

enum TrafficLight {
    Red,
    Yellow,
    Green,
}

use TrafficLight::*;

fn main() {
    let red = Red;
    let yellow = Yellow;
    let green = Green;
}

testしてみる。
testsはsuper::testsモジュールが定義されている。
clientやnetworkモジュールはtestsの親モジュール経由でアクセスできる.
それはsuper::でアクセスできる

// lib.rs

pub mod client;
pub mod network;

# [cfg(test)]
mod tests{
    
    use super::client;

    #[test]
    fn it_works(){
        client::connect();
    }
}

次回!

イエス!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?