rust服务器(了解Rust)

rust有自己的规则和约定用来组织模块,比如一个包最多可以有一个库crate,任意多个二进制crate、导入文件夹内的模块的两种约定方式… 知道这些约定,就可以快速了解rust的模块系统。先把一些术语说明一下:包是cargo的一个功能,当执行cargo new xxxx的时候就是创建了一个包。crate是二进制或者库项目。rust约定在Cargo.toml的同级目录下包含src目录并且包含main.rs文件,就是与包同名的二进制crate,如果包目录中包含src/lib.rs,就是与包同名的库crate。包内可以有多crate,多个crates就是一个模块的树形结构。如果一个包内同时包含src/main.rs和src/lib.rs,那么他就有两个crate,如果想有多个二进制crate,rust约定需要将文件放在src/bin目录下,每个文件就是一个单独的crate。crate根用来描述如何构建crate的文件。比如src/main.rs或者src/lib.rs就是crate根。crate根文件将由Cargo传递给rustc来实际构建库或者二进制项目。带有Cargo.toml文件的包用来描述如何构建crate,一个包可以最多有一个库crate,任意多个二进制crate。模块模块以mod开头,下面创建了一个say模块mod say {
pub fn hello() {
println!("Hello, world!");
}
}需要注意的是模块内,所有的项(函数、方法、结构体、枚举、模块和常量)默认都是私有的,可以用pub将项变为公有,上面的代码里pub fn hello()就是把函数hello()变为公有的。子模块可以通过super访问父模块中所有的代码,包括私有代码。但是父模块中的代码不能访问子模块中的私有代码。mod say {
pub fn hello() {
println!("Hello, world!");
}
fn hello_2() {
println!("hello")
}
pub mod hi {
pub fn hi_1() {
super::hello_2();
}
pub fn hi_2() {
println!("hi there");
}
}
}同一文件内的模块同一文件内的模块,最外层的mod say不用设置为pub就可以访问,但是mod say下面的要设置成pub才可以访问。fn main() {
// 相对路径
say::hello();
// 绝对路径调用
crate::say::hello();

say::hi::hi_1();
say::hi::hi_2();
}

mod say {
pub fn hello() {
println!("Hello, world!");
}
fn hello_2() {
println!("hello")
}
pub mod hi {
pub fn hi_1() {
super::hello_2();
}
pub fn hi_2() {
println!("hi there");
}
}
}调用模块内的方法,可以使用绝对路径以crate开头,也就是从crate根开始查找,say模块定义在crate根 src/main.rs中,所以就可以这么调用crate::say::hello();绝对路径类似于Shell中使用/从文件系统根开始查找文件。相对路径以模块名开始say,他定义于main()函数相同的模块中,类似Shell在当前目录开始查找指定文件say/hello。mod hi是一个嵌套模块,使用时要写比较长say::hi::hi_2();,可以使用use将名称引入作用域。use crate::say::hi;

fn main() {
// 相对路径
say::hello();
// 绝对路径调用
crate::say::hello();

// 不使用 use
say::hi::hi_1();
say::hi::hi_2();
// 使用 use 后就可以这么调用
hi::hi_1();
}Rust相关文档学习资料点击 正在跳转 获取使用pub use重导出名称不同的模块之前使用use引入,默认也是私有的。如果希望调用的模块内use引用的模块,就要用pub公开,也叫重导出fn main() {
// 重导出名称
people::hi::hi_1();
people::hello();
// 但是不能
// people::say::hello();
}

mod say {
pub fn hello() {
println!("Hello, world!");
}
fn hello_2() {
println!("hello")
}
pub mod hi {
pub fn hi_1() {
super::hello_2();
}
pub fn hi_2() {
println!("hi there");
}
}
}

mod people {
// 重导出名称
pub use crate::say::hi;
use crate::say;
pub fn hello() {
say::hello();
}
}如果想都导出自己和嵌入的指定包可以用self,例mod people_2 把模块people和嵌套模块info全部导出来了。use crate::say::hi;

fn main() {
// 相对路径
say::hello();
// 绝对路径调用
crate::say::hello();

// 不使用 use
say::hi::hi_1();
say::hi::hi_2();
// 使用 use 后就可以这么调用
hi::hi_1();

// 重导出名称
people::hi::hi_1();
people::hello();
// 但是不能
// people::say::hello();

people_2::people::hello();
people_2::info::name();
}

mod say {
pub fn hello() {
println!("Hello, world!");
}
fn hello_2() {
println!("hello")
}
pub mod hi {
pub fn hi_1() {
super::hello_2();
}
pub fn hi_2() {
println!("hi there");
}
}
}

pub mod people {
// 重导出名称
pub use crate::say::hi;
use crate::say;
pub fn hello() {
say::hello();
}
pub mod info {
pub fn name() {
println!("zhangsang");
}
}
}

mod people_2 {
// 重导出名称
pub use crate::people::{self, info};
pub fn hello() {
info::name();
}
}
不同文件夹的引用方式一看一下目录结构:rust的约定,在目录下使用mod.rs将模块导出。看一下user.rs的代码:#[derive(Debug)]
pub struct User {
name: String,
age: i32
}

impl User {
pub fn new_user(name: String, age: i32) -> User {
User{
name,
age
}
}
pub fn name(&self) -> &str {
&self.name
}
}

pub fn add(x: i32, y: i32) -> i32 {
x + y
}然后在mod.rs里导出:pub mod user;在main.rs调用mod user_info;
use user_info::user::User;

fn main() {
let u1 = User::new_user(String::from("tom"), 5);
println!("user name: {}", u1.name());
println!("1+2: {}", user_info::user::add(1, 2));
}
方式二看一下目录结构和上面的不同之前是。这种方式是user_info目录里没有mod.rs,但是在外面有一个user_info.rs在user_info.rs中使用pub mod user;是告诉Rust在另一个与模块同名的文件夹内(user_info文件夹)内加载模块user。这也是rust的一个约定,但比较推荐用上面的方式。代码和上面是一样的。user.rs#[derive(Debug)]
pub struct User {
name: String,
age: i32
}

impl User {
pub fn new_user(name: String, age: i32) -> User {
User{
name,
age
}
}
pub fn name(&self) -> &str {
&self.name
}
}

pub fn add(x: i32, y: i32) -> i32 {
x + y
}user_info.rs里导出pub mod user;在main.rs调用mod user_info;
use user_info::user::User;

fn main() {
let u1 = User::new_user(String::from("tom"), 5);
println!("user name: {}", u1.name());
println!("1+2: {}", user_info::user::add(1, 2));
}
使用外部包使用外部包,一般就是从crates.io下载,当然也可以自己指写下载地点,或者使用我们本地的库,或者自建的的仓库。一般方式在Cargo.toml的dependencies下写要导入的依赖库[dependencies]
regex = "0.1.41"运行cargo build会从crates.io下载依赖库。使用的时候,直接使用use引入use regex::Regex;

fn main() {
let re = Regex::new(r"^\d{4}-\d{2}-\d{2}#34;).unwrap();
println!("Did our date match? {}", re.is_match("2014-01-01"));
}指定库地址除了crates.io下载依赖库,也可以自己指定地址,也可以指定branch tag commit,比如下面这个[dependencies]
# 可以和包不同名,也可以同名
my_rust_lib_1={package="my_lib_1",git="ssh://[email protected]/lpxxn/my_rust_lib_1.git",tag="v0.0.2"}就是从github.com/lpxxn/my_rust_lib_1上下载包。也可以使用httpsmy_rust_lib_1={package="my_lib_1",git="https://github.com/lpxxn/my_rust_lib_1.git",branch="master"}执行cargo build就会自动下载,使用的时候也是一样的。use my_rust_lib_1;
fn main() {
println!("Hello, world!");
println!("{}", my_rust_lib_1::add(1, 2));
let u = my_rust_lib_1::User::new_user(String::from("tom"), 2);
println!("user: {:#?}", u);
}
使用本地的库我们新建一个二进制库项目cargo new pkg_demo_3然后在pkg_demo_3内建一个库项目cargo new –lib utils然后就可以在 utils里写我们的库代码了看一下现在的目录结构在utils库的user.rs里还是我们上面的代码#[derive(Debug)]
pub struct User {
name: String,
age: i32
}

impl User {
pub fn new_user(name: String, age: i32) -> User {
User{
name,
age
}
}
pub fn name(&self) -> &str {
&self.name
}
}

pub fn add(x: i32, y: i32) -> i32 {
x + y
}在lib.rs里对user模块导出pub mod user;
pub use user::User;然后在我们的二进制库的Cargo.toml引入库[dependencies]
utils = { path = "utils", version = "0.1.0" }path就是库项目的路径main.rs使用use引入就可以使用了use utils::User;

fn main() {
let u = User::new_user(String::from("tom"), 5);
println!("user: {:#?}", u);
}

本文出自快速备案,转载时请注明出处及相应链接。

本文永久链接: https://www.175ku.com/26869.html