当前位置: 首页 > 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(){}
http://www.lryc.cn/news/225090.html

相关文章:

  • RPA在财务预测和分析中的应用
  • 无人机航拍技术基础入门,无人机拍摄的方法与技巧
  • PTA 哈密尔回路(建图搜索)
  • 如何利用产品帮助中心提升用户体验
  • 【Python大数据笔记_day05_Hive基础操作】
  • css呼吸效果实现
  • 机器视觉opencv答题卡识别系统 计算机竞赛
  • 2024年的后端和Web开发趋势
  • 对比了10+网盘资源搜索工具,我最终选择了这款爆赞的阿里云盘、百度网盘、夸克网盘资源一站式搜索工具
  • GoLong的学习之路(二十)进阶,语法之反射(reflect包)
  • 关于表单校验,:rules=“loginRules“
  • 统一消息分发中心设计
  • 前端项目导入vue和element
  • 【11】使用透视投影建立一个3D空间的测试
  • 【广州华锐互动】VR影视制片虚拟仿真教学系统
  • 从研发域到量产域的自动驾驶工具链探索与实践
  • 404. 左叶子之和
  • 基于SSM的课程管理系统
  • 【hcie-cloud】【5】华为云Stack规划设计之华为云Stack标准化配置、缩略语【下】
  • 搭建自己的MQTT服务器,实现设备上云(Ubuntu+EMQX)
  • web3案例中解决交易所中 ETH与token都是0问题 并帮助确认展示是否成功
  • unreal engine oculus 在vr场景中fade in , fade out
  • 0. 前言与大纲
  • 家乡特色饮食体验系统的设计与实现-计算机毕设 附源码 27533
  • 本地数据库迁移到云端服务器
  • SpringCloudGateway--Sentinel限流、熔断降级
  • ARMday02(汇编语法、汇编指令)
  • docker容器中运行jar 出现invalid or corrupt jarfile
  • PHP+MySQL人才招聘小程序系统源码 带完整前端+后端搭建教程
  • MongoDB常用的语句