rust学习(6)-关于模块引入作用域,宏引入作用域

1 宏导入作用域

#[macro_use]
extern crate serde_derive;

2 item导入作用域(目的是缩短模块路径)

没有使用第三方包,不用加包的依赖
//use std::sync::Arc;
pub struct ProbeClient {
    user: String,
    cookies: String,
}
fn main() {//std包和core包都内置到rust语言了,不用加依赖
    //Arc这个item来自std库,如果不用引入路径,但是需要在写全Arc这个item的模块路径,不然rustc编译器找不到这个item
    let arc = std::sync::Arc::new(ProbeClient{
        user: "username".to_string(),
        cookies:"324889fddgadwq".to_string(),
    });
    println!("{}",arc.user); //Arc<T>是智能指针
    //用core包下的东西不用引入作用域达到缩短书写模块路径的目的,也不用在使用时写全chars()这个item的模块路径,编译器可以找到
    let mut chars = "abc".chars();    //chars()这个方法是在core::str下 ,意思是core这个根模块(这个模块很特殊,对应的是src/lib.rs文件)下的str模块里的东西
}

使用core 包里的东西,不用加依赖,也不用导入作用域, 使用std 包里的东西,不用加依赖,但是需要使用use 语句把item导入作用域,否则需要从std包对应的这个根模块std开始写直到item,使用第三方包必须加依赖否则用不了,core包相当于std包的精华版。

猜你喜欢

转载自blog.csdn.net/qq_40642465/article/details/120386733