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

rust feature h和 workspace相关知识 (十一)

feature 相关作用和描述

在 Rust 中,features(特性) 是一种控制可选功能和依赖的机制。它允许你在编译时根据不同的需求启用或禁用某些功能,优化构建,甚至改变代码的行为。Rust 的特性使得你可以轻松地为库提供不同的配置,或者在不同的环境中使用不同的功能

1.新建一个项目 study_feature_2025_1_23

Cargo.toml

[package]
name = "study_feature_2025_1_23"
version = "0.1.0"
edition = "2021"
publish = ["crates-io"][dependencies]
serde = {version = "1.0.217", features = ["derive"],optional = true}
rgb = {version = "0.8.43", features = ["serde"],optional = true}[features]
color = ["dep:rgb"] # 依赖 rgb 项目 
shapes = ["color","dep:serde","rgb?/serde"] # 依赖加载 color,serde, 如果使用了 rgb 则加载 serde 特性
default = ["color"] # 默认开启 color feature
  • lib.rs 代码如下
pub fn draw_line(x: i32, y: i32) {println!("Drawing a line at ({}, {})", x, y);
}#[cfg(feature = "color")] // 这个是设置这个模块为一个feature : color 
pub mod color {pub use rgb::RGB;pub fn draw_line(x: i32,y: i32, color: &RGB<u16>){println!("{color}, Drawing a line at ({}, {})", x, y);}
}#[cfg(feature = "shapes")] // 这个是设置这个模块为一个feature : shapes 
pub mod shapes {use serde::{Deserialize, Serialize};use rgb::RGB;#[derive(Debug,Serialize,Deserialize)]pub struct Rectangle {pub width: u32,pub height: u32,pub color: RGB<u16>,}}
2.新建一个项目 study_feature_consumer_2025_1_23

Cargo.toml

[package]
name = "study_feature_consumer_2025_1_23"
version = "0.1.0"
edition = "2021"
publish = ["crates-io"][dependencies]
study_feature_2025_1_23 = {path = "../study_feature_2025_1_23",default-features = false,features = ["shapes"]}
# default-features  是false 关闭所有默认feature
  • main.rs
use study_feature_2025_1_23::color;
use study_feature_2025_1_23::shapes;fn main() {study_feature_2025_1_23::draw_line(1, 2);let color = color::RGB {r: 0,g: 0,b: 0,};color::draw_line(1, 2, &color);println!("形状: {:?}", shapes::Rectangle {width: 1,height: 2,color,});
}
workspace
[workspace]
resolver = "2"
members = ["hecto3_8","hecto3_9","hecto3_10","hecto3_11","hecto4_1","hecto4_3","hecto4_4","hecto4_5","hecto4_6","hecto4_7","hecto4_8","hecto4_9","hecto5_1","hecto5_2","hecto5_3","hecto5_4","hecto5_5","hecto5_6","hecto5_7","hecto5_8","hecto5_9","hecto5_10","hecto5_11",
][workspace.dependencies] # 依赖的全局包
env_logger = "0.11.3"[profile.release]
lto = true
opt-level = "z"
panic = "abort"
# 编译器能进行全局优化
codegen-units = 1[profile.dev]
http://www.lryc.cn/news/526304.html

相关文章:

  • -bash: ./uninstall.command: /bin/sh^M: 坏的解释器: 没有那个文件或目录
  • 【Redis】Redis入门以及什么是分布式系统{Redis引入+分布式系统介绍}
  • C#高级:常用的扩展方法大全
  • Consul持久化配置报错1067---consul_start
  • 「 机器人 」扑翼飞行器控制策略浅谈
  • Qt信号与槽底层实现原理
  • QT QTableWidget控件 全面详解
  • Flutter_学习记录_基本组件的使用记录
  • 基于JAVA的微信点餐小程序设计与实现(LW+源码+讲解)
  • 计算机毕业设计hadoop+spark+hive民宿推荐系统 酒店推荐系统 民宿价格预测 酒店价格 预测 机器学习 深度学习 Python爬虫 HDFS集群
  • 亲测有效!解决PyCharm下PyEMD安装报错 ModuleNotFoundError: No module named ‘PyEMD‘
  • Gin 应用并注册 pprof
  • Jenkins 启动
  • 第20篇:Python 开发进阶:使用Django进行Web开发详解
  • 文献引用指南ChatGPT提示词分享
  • 程序代码篇---C++类.c\.h
  • @RabbitListener处理重试机制完成后的异常捕获
  • Mac 上管理本地 Go 版本
  • 低代码系统-产品架构案例介绍、得帆云(八)
  • 免费GPU算力,不花钱部署DeepSeek-R1
  • JavaEE:多线程进阶
  • 不只是mini-react第二节:实现最简fiber
  • C++实现设计模式---命令模式 (Command)
  • 设计模式的艺术-享元模式
  • Linux的权限和一些shell原理
  • 【Postgres_Python】使用python脚本批量创建和导入多个PG数据库
  • Ubuntu安装GitLab
  • 网络知识小科普--5
  • JavaScript学习记录23
  • VScode 开发 Springboot 程序