Rust--模块

Rust中要实现多个文件来编译程序。需要学习以下内容来实现:

Rust中的组织单位是模块,就像CSharp语言中的组织单位是类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
pub mod school{
    pub mod teacher{
       pub fn teach(){
            println!("Teacher's work is teaching student");
        }
    }
 
    pub mod student{
     pub   fn study(){
            println!("Student's work is studying");
        }
    }
}
 
use school::teacher;
fn main(){
     
    school::student::study();
     
 
    teacher::teach();
     
}

 输出结果:

 

 

 标准库文件调用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
pub mod school{
    pub mod teacher{
       pub fn teach(){
            println!("Teacher's work is teaching student");
        }
    }
 
    pub mod student{
     pub   fn study(){
            println!("Student's work is studying");
        }
    }
}
 
use school::teacher;
use std::f64::consts::PI as pi;  //调用标准库文件
fn main(){   
    school::student::study();
    teacher::teach();
    println!("pi number is {}",pi);   
}

 输出结果:

 

Rust标准库:https://doc.rust-lang.org/stable/std/all.html

 

posted @   echo-efun  阅读(103)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
点击右上角即可分享
微信分享提示