rust使用print控制台打印输出五颜六色的彩色红色字体

想要在控制台打印输出彩色的字体,可以使用一些已经封装好的依赖库,比如ansi_term这个依赖库,官方依赖库地址:https://crates.io/crates/ansi_term

安装依赖:

rust 复制代码
cargo add ansi_term

或者在Cargo.toml文件中加入:

rust 复制代码
[dependencies]
ansi_term = "0.12"

使用ansi_term,我们可以很容易地在Rust中使用彩色文本。下面是一个简单的示例代码:

rust 复制代码
use ansi_term::Colour::{Red, Green, Yellow, Blue, Purple, Cyan};

fn main() {
    let err_msg = "这是错误消息";
    println!("{} this is red", Red.paint(format!("ERROR MESSAGE: {}", err_msg)));
    println!("{} this is green", Green.paint("SUCCESS:"));
    println!("{} this is yellow", Yellow.paint("WARNING:"));
    println!("{} this is blue", Blue.paint("INFO:"));
    println!("{} this is purple", Purple.paint("DEBUG:"));
    println!("{} this is cyan", Cyan.paint("TRACE:"));
}

输出效果:

相关推荐
维吉斯蔡4 小时前
【VS Code / Cursor】文件夹右键快捷打开与文件类型自动关联
开发语言·程序人生·学习方法
前端工作日常4 小时前
我学习到的Java 的 Service 分层:itf 和 impl 到底是什么?
java·后端
前端工作日常5 小时前
我学习到的JIT即时编译与机器码缓存失效
java·后端
犀利豆6 小时前
Claude Code Tools 研究系列(三)ExitPlanMode
人工智能·后端
luj_17686 小时前
星火科技助力边远地区防病攻坚
c语言·开发语言·c++·经验分享·算法
always_TT6 小时前
【Python 日志记录:logging 模块入门】
开发语言·python·php
星栈6 小时前
oh-my-pi工程级AI编码工使用体验
人工智能·后端·agent
xcLeigh6 小时前
Go入门:变量声明的五种方式详解
java·开发语言·golang
zmzb01037 小时前
C++课后习题训练记录Day175
开发语言·c++
花褪残红青杏小7 小时前
Rust图像处理第24节- 噪声模型 + 中心极限定理:椒盐噪声 + CLT 可视化
rust·webassembly·图形学