当前位置: 首页 > news >正文

Rust4.1 Managing Growing Projects with Packages, Crates, and Modules

Rust学习笔记

Rust编程语言入门教程课程笔记

参考教材: The Rust Programming Language (by Steve Klabnik and Carol Nichols, with contributions from the Rust Community)

Lecture 7: Managing Growing Projects with Packages, Crates, and Modules

src/main.rs

// src/main.rs: the main file of the project, where the main function is defined; this is the crate root
// src/lib.rs: the root of your crate’s library; the library’s name is the same as the name of the crate
// src/bin: directory that can contain multiple binary crates; each file in this directory will be a separate binary crate
// src/bin/main.rs: the main file of the binary crate with the same name as the directory; this file is the crate root of the binary crate//Modules
use std::collections::HashMap;// use keyword to bring module into scopeuse std::io::Result as IoResult;// use keyword to bring module into scope
use std::{cmp, io};// use keyword to bring module into scope
use rand::Rng;// use keyword to bring module into scope
use std::collections::*;// use keyword to bring module into scope, * is glob operatorfn main() {let mut map = HashMap::new();map.insert(1, 2);println!("{:?}", map);let m = IoResult::Ok(());println!("{:?}", m);let mut rng = rand::thread_rng();let t = rng.gen_range(1..=10);println!("{:?}", t);
}

src/lib.rs

mod front_of_house;//{// module// pub mod hosting{// public module//     pub fn add_to_waitlist(){}// }// mod serving{// private module//     fn take_order(){}//     fn serve_order(){}//     fn take_payment(){}// }// fn fix_incorrect_order(){//     cook_order();//     super::serve_order();// super keyword to access parent module//     crate::serve_order();// absolute path// }// pub fn cook_order(){}// pub struct Breakfast{//     pub toast: String,//     seasonal_fruit: String,// }// impl Breakfast{//     pub fn summer(toast: &str) -> Breakfast{//         Breakfast{//             toast: String::from(toast),//             seasonal_fruit: String::from("peaches"),//         }//     }// }
//}pub use crate::front_of_house::hosting;// use keyword to bring module into scope
//use crate::front_of_house::servering;// cannot use private module
//use front_of_house::hosting;// relative pathpub fn eat_at_restaurant(){// Absolute pathcrate::front_of_house::hosting::add_to_waitlist();// Relative pathfront_of_house::hosting::add_to_waitlist();// Order a breakfast in the summer with Rye toastlet mut meal = front_of_house::Breakfast::summer("Rye");// Change our mind about what bread we'd likemeal.toast = String::from("Wheat");println!("I'd like {} toast please", meal.toast);// The next line won't compile if we uncomment it; we're not allowed// to see or modify the seasonal fruit that comes with the meal// meal.seasonal_fruit = String::from("blueberries");hosting::add_to_waitlist();}fn serve_order(){}

src/front_of_house.rs

pub mod hosting; //{// public module
//     pub fn add_to_waitlist(){}
// }mod serving{// private modulefn take_order(){}fn serve_order(){}fn take_payment(){}
}fn fix_incorrect_order(){cook_order();super::serve_order();// super keyword to access parent modulecrate::serve_order();// absolute path
}pub fn cook_order(){}pub struct Breakfast{pub toast: String,seasonal_fruit: String,
}impl Breakfast{pub fn summer(toast: &str) -> Breakfast{Breakfast{toast: String::from(toast),seasonal_fruit: String::from("peaches"),}}
}

src/front_of_house/hosting.rs

pub fn add_to_waitlist(){}

相关文章:

  • python之SPC:计算Cpk
  • 2023.11.10联赛 T4题解
  • 一个“Hello, World”Flask应用程序
  • 冲突域、广播域、一些网络设备
  • 大容量疯了!居然想把磁带放到硬盘,100TB+是否可以实现?
  • 【Git】Git 学习笔记_操作远程仓库
  • ZYNQ_project:key_breath
  • 【实战-08】flink DataStream 如何实现去重
  • 技术分享 | Appium 用例录制
  • GZ038 物联网应用开发赛题第1套
  • 全志A40i应用笔记 | 3种常见的网卡软件问题以及排查思路
  • 前端-第一部分-HTML
  • pytorch(小土堆)深度学习
  • Python语法基础(条件语句 循环语句 函数 切片及索引)
  • 软件测试|MySQL BETWEEN AND:范围查询详解
  • Docker容器管理
  • JavaScript设计模式系列一:工厂模式
  • jQuery(一)
  • leetcode388. Longest Absolute File Path
  • PhantomJS 安装
  • SpringCloud(第 039 篇)链接Mysql数据库,通过JpaRepository编写数据库访问
  • Spring声明式事务管理之一:五大属性分析
  • vue.js框架原理浅析
  • 第十八天-企业应用架构模式-基本模式
  • 入手阿里云新服务器的部署NODE
  • 网页视频流m3u8/ts视频下载
  • ​ubuntu下安装kvm虚拟机
  • (2.2w字)前端单元测试之Jest详解篇
  • (Bean工厂的后处理器入门)学习Spring的第七天
  • (ctrl.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MDd_DynamicDebug”不匹配值“
  • (M)unity2D敌人的创建、人物属性设置,遇敌掉血
  • (python)数据结构---字典
  • (zhuan) 一些RL的文献(及笔记)
  • (ZT)一个美国文科博士的YardLife
  • (附源码)springboot家庭财务分析系统 毕业设计641323
  • (附源码)计算机毕业设计SSM疫情社区管理系统
  • (利用IDEA+Maven)定制属于自己的jar包
  • (数位dp) 算法竞赛入门到进阶 书本题集
  • (转)从零实现3D图像引擎:(8)参数化直线与3D平面函数库
  • (转)关于如何学好游戏3D引擎编程的一些经验
  • *** 2003
  • .net Application的目录
  • .NET CORE 3.1 集成JWT鉴权和授权2
  • .net core 6 使用注解自动注入实例,无需构造注入 autowrite4net
  • .net on S60 ---- Net60 1.1发布 支持VS2008以及新的特性
  • .net 托管代码与非托管代码
  • .NET/C# 使窗口永不激活(No Activate 永不获得焦点)
  • /etc/sudoer文件配置简析
  • ::before和::after 常见的用法
  • @manytomany 保存后数据被删除_[Windows] 数据恢复软件RStudio v8.14.179675 便携特别版...
  • [2]十道算法题【Java实现】
  • [2017][note]基于空间交叉相位调制的两个连续波在few layer铋Bi中的全光switch——
  • [BZOJ5250][九省联考2018]秘密袭击(DP)
  • [C++基础]-入门知识
  • [CLickhouse] 学习小计