提交 b0ff8432 编写于 作者: 梦境迷离's avatar 梦境迷离

add module

上级 d39557bd
......@@ -3,7 +3,7 @@ name = "rust-examples"
version = "0.1.0"
authors = ["梦境迷离 <liguobin@growingio.com>"]
edition = "2018"
#build = "build.rs" 构建前操作
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
......@@ -7,11 +7,13 @@ use crate::custom_types::*;
use crate::flow_control::*;
use crate::formatted_print::*;
use crate::functions::*;
use crate::modules::*;
use crate::primitives::*;
use crate::types::*;
use crate::variable_bindings::*;
pub mod formatted_print;
pub mod modules;
pub mod functions;
pub mod flow_control;
pub mod closures;
......@@ -31,4 +33,5 @@ fn main() {
flow_control();
functions();
closures();
modules();
}
///模块
pub fn modules() {
struct_visibility();
cfg();
}
fn struct_visibility() {
mod my {
//公有结构体和公有字段
pub struct OpenBox<T> {
pub contents: T,
}
//公有结构体和私有字段
#[allow(dead_code)]//禁用编译器的 未使用警告
pub struct ClosedBox<T> {
contents: T,
}
impl<T> ClosedBox<T> {
//公有的构造函数
pub fn new(contents: T) -> ClosedBox<T> {
ClosedBox {
contents: contents,
}
}
}
}
let open_box = my::OpenBox { contents: "public information" };
println!("The open box contains: {}", open_box.contents);
//ERROR
//let closed_box = my::ClosedBox { contents: "classified information" };
//使用构造函数可以
let _closed_box = my::ClosedBox::new("classified information");
//ERROR
//println!("The closed box contains: {}", _closed_box.contents);
}
fn use_declaration() {
//指定别名
// use deeply::nested::function as other_function;
// other_function();
//相对路径,父级和当前级
//super 和 self
}
//extern crate rary; 导入library库
fn cfg() {
// This function only gets compiled if the target OS is linux
#[cfg(target_os = "linux")]
fn are_you_on_linux() {
println!("You are running linux!");
}
// And this function only gets compiled if the target OS is *not* linux
#[cfg(not(target_os = "linux"))]
fn are_you_on_linux() {
println!("You are *not* running linux!");
}
are_you_on_linux();
println!("Are you sure?");
if cfg!(target_os = "linux") {//target_os由rustc隐式提供,但是自定义条件条件必须使用--cfg标志传递给rustc
println!("Yes. It's definitely linux!");
} else {
println!("Yes. It's definitely *not* linux!");
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册