Rust7.2 More About Cargo and Crates.io

news/2025/2/2 3:52:19/

Rust学习笔记

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

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

Lecture 14: More About Cargo and Crates.io

main.rs

//Customizing Builds with Release Profiles
// cargo profile:
// 1. dev profile: cargo build
// 2. release profile: cargo build --release// customizing release profile
// cargo.toml: profile.release or profile.dev// Filename: Cargo.toml
// [profile.dev]
// opt-level = 0 // no optimization// [profile.release]
// opt-level = 3 // more optimization, slower compile time// use cargo_and_crateio::kinds::PrimaryColor;
// use cargo_and_crateio::utils::mix;use cargo_and_crateio::PrimaryColor;
use cargo_and_crateio::mix;fn main() {let red = PrimaryColor::Red;let yellow = PrimaryColor::Yellow;mix(red, yellow);
}

lib.rs

//Publishing a Crate to Crates.io
//Making Useful Documentation Comments//Commenting Contained Items
//! # cargo_and_crateio
//!
//! `cargo_and_crateio` is a collection of utilities to make performing certain
//! calculations more convenient./// Adds one to the number given.
///
/// # Examples
///
/// ```
/// let arg = 5;
/// let answer = cargo_and_crateio::add_one(arg);
///
/// assert_eq!(6, answer);
/// ```
pub fn add_one(x: i32) -> i32 {x + 1
}
//cargo doc
//run rustdoc tool to generate HTML documentation from the comments. (in the target/doc directory)
//cargo doc --open
//open documentation in browser//commonly used sections:
// 1. Examples
// 2. Panics
// 3. Errors
// 4. Safety//Documentation Comments as Tests
//cargo test//Exporting a Convenient Public API with pub use//Re-exporting Names with pub use// //! # Art
// //!
// //! A library for modeling artistic concepts.pub use self::kinds::PrimaryColor;
pub use self::kinds::SecondaryColor;
pub use self::utils::mix;pub mod kinds {/// The primary colors according to the RYB color model.pub enum PrimaryColor {Red,Yellow,Blue,}/// The secondary colors according to the RYB color model.pub enum SecondaryColor {Orange,Green,Purple,}
}pub mod utils {use crate::kinds::*;/// Combines two primary colors in equal amounts to create/// a secondary color.pub fn mix(c1: PrimaryColor, c2: PrimaryColor) -> SecondaryColor {// --snip--SecondaryColor::Orange}
}

workplaces

add/Cargo.toml

[workspace]
members = ["adder", "add_one", "add_two"]

add/target/debug: the compliled files

add/adder

Cargo.toml

[package]
name = "adder"
version = "0.1.0"
edition = "2021"[dependencies]
add_one = {path = "../add_one"}

add/adder/src/main.rs

use add_one;fn main() {let num = 10;println!("Hello, world! {} plus one is {}!", num, add_one::add_one(num));
}

add/add_one/src/lib.rs

pub fn add(left: usize, right: usize) -> usize {left + right
}pub fn add_one(num: usize) -> usize {num + 1
}#[cfg(test)]
mod tests {use super::*;#[test]fn it_works() {let result = add(2, 2);assert_eq!(result, 4);}#[test]fn it_adds_one() {let result = add_one(2);assert_eq!(result, 3);}
}

add/add_two/src/lib.rs

pub fn add(left: usize, right: usize) -> usize {left + right
}pub fn add_two(num: usize) -> usize {num + 2
}#[cfg(test)]
mod tests {use super::*;#[test]fn it_works() {let result = add(2, 2);assert_eq!(result, 4);}#[test]fn it_adds_two() {let result = add_two(2);assert_eq!(result, 4);}
}

test the whole workplaces: run “cargo test” in the add dictionary


http://www.ppmy.cn/news/1226937.html

相关文章

用5000字讲清楚压敏电阻

大家好,这里是大话硬件。 今天这篇文章用5000字的篇幅讲清楚压敏电阻。 1. 压敏电阻 压敏电阻,英文名Voltage Dependent Resistor,缩写VDR,或者叫Varistor,Variable(会变的)+ Resistor(电阻)。它的伏安特性曲线具有非线性。也就是压敏电阻的阻值并不是固定的,存在…

数据库mysql详细教学

目录 mysql的第一组基本操作:数据库操作 1、查看当前数据库 2、创建数据库 3、选中数据库 4、删除数据库 5、表操作 5.1查看数据库中的表 ​编辑 5.2创建表 5.2.1数据类型 5.3 查看指定表的表结构 5.4删除表 5.5 MySQL表的增删改查 5.5.1新增 / 插入数据…

Flutter 3.16 中带来的更新

Flutter 3.16 中带来的更新 目 录 1. 概述2. 框架更新2.1 Material 3 成为新默认2.2 支持 Material 3 动画2.3 TextScaler2.4 SelectionArea 更新2.5 MatrixTransition 动画2.6 滚动更新2.7 在编辑菜单中添加附加选项2.8 PaintPattern 添加到 flutter_test 3. 引擎更新&#xf…

二叉树最近公共祖先

题目顺序 01 236. 二叉树的最近公共祖先 02 235. 二叉搜索树的最近公共祖先 1644. 二叉树的最近公共祖先 II 1650. 二叉树的最近公共祖先 III 1676. 二叉树的最近公共祖先 IV🔒

Docker在Centos7下的安装

1、卸载旧版本 执行如下指令对旧版本进行卸载: sudo yum remove docker \docker-client \docker-client-latest \docker-common \docker-latest \docker-latest-logrotate \docker-logrotate \docker-engine 执行完毕后,如果输入docker version发现do…

基于枚举实现的桥接模式

基于枚举实现的桥接模式 这里是基于枚举方式来维护的桥接模式,维护抽象化和实现化的关系。 桥接模式的应用场景: 当一个类内部具备两种或多种变化维度时,使用桥接模式可以解耦这些变化的维度,使高层代码架构稳定。 桥接模式通常…

鸿蒙:Harmony开发基础知识详解

一.概述 工欲善其事,必先利其器。 上一篇博文实现了一个"Hello Harmony"的Demo,今天这篇博文就以Demo "Hello Harmony" 为例,以官网开发文档为依据,从鸿蒙开发主要的几个方面入手,详细了解一下鸿…

excel中设置图表图例的位置

例如,在excel中已经做好了一个折线图: 可以看到,默认图例是在图表的右侧,减小了图的横向展示区域。我们可以把图例放到图的上边、或者下边。 双击图表: 鼠标放在图例上方,出现了浮动文字“图例”&#…