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

输出效果:

相关推荐
通信仿真实验室23 分钟前
(10)MATLAB莱斯(Rician)衰落信道仿真1
开发语言·matlab
勿语&26 分钟前
Element-UI Plus 暗黑主题切换及自定义主题色
开发语言·javascript·ui
2401_857622663 小时前
SpringBoot框架下校园资料库的构建与优化
spring boot·后端·php
2402_857589364 小时前
“衣依”服装销售平台:Spring Boot框架的设计与实现
java·spring boot·后端
吾爱星辰4 小时前
Kotlin 处理字符串和正则表达式(二十一)
java·开发语言·jvm·正则表达式·kotlin
ChinaDragonDreamer4 小时前
Kotlin:2.0.20 的新特性
android·开发语言·kotlin
IT良4 小时前
c#增删改查 (数据操作的基础)
开发语言·c#
哎呦没5 小时前
大学生就业招聘:Spring Boot系统的架构分析
java·spring boot·后端
Kalika0-05 小时前
猴子吃桃-C语言
c语言·开发语言·数据结构·算法
_.Switch5 小时前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j