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.mzph.cn/news/137573.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Unity插件】分享几个完全免费的2D角色动画生成器(推荐收藏)

文章目录 前言一、lpc-character-generator二、Universal-LPC-Spritesheet-Character-Generator三、UP主开发的2D人物换装系统四、Character Editor: Megapack完结 前言 你可能游戏开发能力很强,但是正愁于2D角色动画,那么这篇文章就是为你而准备的&…

C++之list的用法介绍

C之list的用法介绍 1&#xff09;定义和初始化&#xff1a; #include <list> std::list<int> my_list; // 定义一个整数类型的list std::list<std::string> my_other_list {"apple", "banana", "cherry"}; // 初始化一个…

RISC Zero的Bonsai证明服务

1. 引言 Bonsai为通用ZKP网络&#xff0c;其支持任意链、任意协议、以及任意应用&#xff0c;利用ZKP来扩容、隐私和互操作。Bonsai的目标是为每条链都提供无限计算的能力。 借助Bonsai&#xff0c;可仅需数天的开发&#xff0c;即可实现对以太坊、L1链、Cosmos app链、L2 ro…

【FastCAE源码阅读7】视图方向切换按钮实现原理

在FastCAE工具栏上有视图切换按钮&#xff0c;如下图所示&#xff1a; 本文介绍如何实现。 FastCAE集成了Python解析器&#xff0c;当单击按钮时&#xff0c;中间用Python执行的&#xff0c;最后调用MainWindow.dll库接口实现的。 具体的Python代码在Python模块的py文件夹下的…

react:swr接口缓存

useSWR 是一个 React Hooks&#xff0c;是 HTTP 缓存库 SWR 的核心方法之一。SWR 是一个轻量级的 React Hooks 库&#xff0c;通过自动缓存数据来实现 React 的数据获取。 第一个参数是被缓存的数据的 key&#xff0c; 第二个参数是一个函数&#xff0c;该函数返回数据或者一个…

Postman汉化教程

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 Postman汉化教程 前言 前言 Postman是一款支持http协议的接口调试与测试工具&#xff0c;其主要特点就是功能强大&#xff0c;使用简单且易用性好 。无论是开发人员进行接口…

uniapp小程序才到第五层就报错navigateto:fail webview count limit exceed

错误截图 原因 小程序官方描述是说可以跳转10层&#xff0c;但是使用uniapp开发的程序在小程序中才运行到第五层就报错了&#xff0c;原因是因为没有设置appId。如果设置了就正常了。

FPGA UDP RGMII 千兆以太网(2)IDDR

1 xilinx原语 在 7 系列 FPGA 中实现 RGMII 接口需要借助 5 种原语,分别是:IDDR、ODDR、IDELAYE2、ODELAYE2(A7 中没有)、IDELAYCTRL。其中,IDDR和ODDR分别是输入和输出的双边沿寄存器,位于IOB中。IDELAYE2和ODELAYE2,分别用于控制 IO 口输入和输出延时。同时,IDELAYE2 …

文件缓存的读写

文件系统的读写&#xff0c;其实就是调用系统函数 read 和 write。下面的代码就是 read 和 write 的系统调用&#xff0c;在内核里面的定义。 SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count) {struct fd f fdget_pos(fd); ......loff_t pos f…

数据分析是什么?

第一章- 数据分析是什么 数据分析是指 根据分析目的&#xff0c;用适当的分析方法及工具&#xff0c;对数据进行分析&#xff0c;提取有价值的信息&#xff0c;形成有效结论的过程。 数据分析的作用 通过观察数据&#xff0c;知道当前发生什么&#xff1f;通过具体的数据拆解…

Webpack 中 loader 的作用是什么?常用 loader 有哪些?

说说webpack中常见的Loader&#xff1f;解决了什么问题&#xff1f;- 题目详情 - 前端面试题宝典 1、loader 是什么 loader是 webpack 最重要的部分之一。 通过使用不同的 loader&#xff0c;我们能够调用外部的脚本或者工具&#xff0c;实现对不同格式文件的处理。 loader…

修改angular cli 的默认包管理器

运行 ng new Angular CLI 时默认使用 npm 作为包管理器。在本文中&#xff0c;您将了解如何设置不同的包管理器创建项目。 适用于 Angular 版本 6 及更高版本 配置包管理器 若要 Angular CLI 使用指定的包管理器&#xff0c;可以运行以下命令来使用想要的包管理器&#xff1a;…

嵌入式养成计划-48----QT--信息管理系统:百川仓储管理

一百二十二、信息管理系统&#xff1a;百川仓储管理 122.1 UI界面 122.2 思路 客户端&#xff1a; 用户权限有两种类型&#xff0c;一种是用户权限&#xff0c;一种是管理员权限&#xff0c;登录时服务器端会根据数据库查询到的此用户名的权限返回不同的结果&#xff0c;客户…

Semantic-Guided Zero-Shot Learning for Low-Light ImageVideo Enhancement

论文阅读之无监督低光照图像增强 Semantic-Guided Zero-Shot Learning for Low-Light Image/Video Enhancement 代码&#xff1a; https://github.com/ShenZheng2000/SemantiGuided-Low-Light-Image-Enhancement 在低光条件下增加亮度的一个可行方法是使用更高的ISO或更长时间…

mongodb数据同步到hive

背景 用户需求: 需要将 mongodb 的数据同步到 hive 表&#xff0c;共 2 亿条数据&#xff0c;总数据量约 30G 查阅一些博客后&#xff0c;大致同步方法有以下几种 手动离线 对于比较小的数据&#xff0c;可以先通过 mongoexport 将数据导出到本地 json 文件&#xff0c;再将…

仿mudou库one thread one loop式并发服务器

目录 1.实现目标 2.HTTP服务器 实现高性能服务器-Reactor模型 模块划分 SERVER模块&#xff1a; HTTP协议模块&#xff1a; 3.项目中的子功能 秒级定时任务实现 时间轮实现 正则库的简单使用 通⽤类型any类型的实现 4.SERVER服务器实现 日志宏的封装 缓冲区Buffer…

简单漂亮的登录页面

效果图 说明 开发环境&#xff1a;vue3&#xff0c;sass 代码 <template><div class"container"><div class"card-container"><div class"card-left"><span><h1>Dashboard</h1><p>Lorem ip…

HTTP 协议详解-上(Fiddler 抓包演示)

文章目录 HTTP 协议HTTP 协议的工作过程HTTP 请求 (Request)认识URL关于 URL encode认识 "方法" (method)GET 方法POST 方法其他方法请求 "报头" (header)请求 "正文" (body) HTTP 响应详解状态码响应 "报头" (header) HTTP 协议 HTT…

【编程语言发展史】Unity开发语言的历史发展

Unity开发前期版本时&#xff0c;使用的是一种名为UnityScript的类似JavaScript的语言。然而&#xff0c;随着时间的推移&#xff0c;开发者社区大多数人都倾向于使用C#进行开发&#xff0c;Unity决定将重点放在C#上&#xff0c;因为C#具有更强大的生态系统、更好的性能和更广泛…

Spring Boot 3.0正式发布及新特性解读

目录 【1】Spring Boot 3.0正式发布及新特性依赖调整升级的关键变更支持 GraalVM 原生镜像 Spring Boot 最新支持版本Spring Boo 版本版本 3.1.5前置系统清单三方包升级 Ref 个人主页: 【⭐️个人主页】 需要您的【&#x1f496; 点赞关注】支持 &#x1f4af; 【1】Spring Boo…