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:"));
}

输出效果:

相关推荐
Dxy12393102162 小时前
Python 使用正则表达式将多个空格替换为一个空格
开发语言·python·正则表达式
我学上瘾了2 小时前
Spring Cloud的前世今生
后端·spring·spring cloud
波波0073 小时前
ASP.NET Core 健康检查实战:不只是一个 /health 接口
后端·asp.net
小码哥_常3 小时前
Spring Boot 搭建邮件发送系统:开启你的邮件自动化之旅
后端
故事和你914 小时前
洛谷-数据结构1-1-线性表1
开发语言·数据结构·c++·算法·leetcode·动态规划·图论
石榴树下的七彩鱼4 小时前
图片修复 API 接入实战:网站如何自动去除图片水印(Python / PHP / C# 示例)
图像处理·后端·python·c#·php·api·图片去水印
我叫黑大帅5 小时前
为什么TCP是三次握手?
后端·网络协议·面试
我叫黑大帅5 小时前
如何排查 MySQL 慢查询
后端·sql·面试
techdashen5 小时前
Rust项目公开征测:Cargo 构建目录新布局方案
开发语言·后端·rust
星空椰5 小时前
JavaScript 进阶基础:函数、作用域与常用技巧总结
开发语言·前端·javascript