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

输出效果:

相关推荐
SteveKenny5 分钟前
Python 梯度下降法(六):Nadam Optimize
开发语言·python
Hello.Reader1 小时前
深入浅出 Rust 的强大 match 表达式
开发语言·后端·rust
xrgs_shz3 小时前
MATLAB的数据类型和各类数据类型转化示例
开发语言·数据结构·matlab
customer085 小时前
【开源免费】基于SpringBoot+Vue.JS体育馆管理系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
来恩10036 小时前
C# 类与对象详解
开发语言·c#
komo莫莫da7 小时前
寒假刷题Day19
java·开发语言
ElseWhereR7 小时前
C++ 写一个简单的加减法计算器
开发语言·c++·算法
计算机-秋大田7 小时前
基于微信小程序的电子竞技信息交流平台设计与实现(LW+源码+讲解)
spring boot·后端·微信小程序·小程序·课程设计
※DX3906※8 小时前
cpp实战项目—string类的模拟实现
开发语言·c++