๊ด€๋ฆฌ ๋ฉ”๋‰ด

IT’s Portfolio

[Rust] ๋Ÿฌ์ŠคํŠธ์—์„œ ๋‹ค๋ฅธ ์†Œ์ŠคํŒŒ์ผ ํ•จ์ˆ˜ ์“ฐ๊ธฐ ๋ณธ๋ฌธ

Development Study/Rust

[Rust] ๋Ÿฌ์ŠคํŠธ์—์„œ ๋‹ค๋ฅธ ์†Œ์ŠคํŒŒ์ผ ํ•จ์ˆ˜ ์“ฐ๊ธฐ

f1r3_r41n 2022. 8. 6. 17:28
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’ป Rust Module

  • fn: function
  • pub: public
  • mod: module

๐Ÿ’ก Main ์—์„œ์˜ Import

  • Rust ํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” src ๋””๋ ‰ํ„ฐ๋ฆฌ์— code1 ์†Œ์Šค์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  Import

    • code1

      pub fn code1(){
          println!("import1!");
      }
      • pub fn code1: code1 ํ•จ์ˆ˜ ๊ณต๊ฐœ
    • main

      mod code1;
      
      fn main(){
          code1::code1();
      }
      • mod code1;: ํŒŒ์ด์ฌ์˜ import

๐Ÿ’ก Sub ์—์„œ์˜ Import

  • Rust ํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ํ•˜์œ„ ๋””๋ ‰ํ„ฐ๋ฆฌ algo๋ฅผ ์ƒ์„ฑ ํ›„ ํ•ด๋‹น ๋””๋ ‰ํ„ฐ๋ฆฌ ๋‚ด์— code2์™€ code3 ์†Œ์Šค์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  Import

    • ์„œ๋ธŒ ๋””๋ ‰ํ„ฐ๋ฆฌ ๋‚ด์˜ ์†Œ์Šค๋ฅผ ๊ฐ€์ ธ์˜ค๋ ค๋ฉด ๊ทธ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ๋ฐ˜๋“œ์‹œ mod.rs ํŒŒ์ผ์ด ์žˆ์–ด์•ผ ํ•จ. ๋””๋ ‰ํ„ฐ๋ฆฌ ๋ช…์œผ๋กœ ๋ชจ๋“ˆ์„ ๊ฐ€์ ธ์˜ค๋ ค๊ณ  ์‹œ๋„ํ•˜๋ฉด, ๋ฐ˜๋“œ์‹œ ๊ทธ ์•ˆ์˜ mod.rs๋ฅผ ๊ฐ€์ ธ์˜ค๋ ค๊ณ  ํ•˜๊ธฐ ๋•Œ๋ฌธ

    • code2

      pub fn code2(){
          println!("import1!");
      }
    • code3

      pub fn code3(){
          println!("import1!");
      }
    • mod

      pub mod code2;
      pub use code2::*;
      pub mod code3;
      pub use code3::*;
    • main

      mod algo;
      
      fn main(){
          algo::code2();
          algo::code3();
      }
728x90
๋ฐ˜์‘ํ˜•
Comments