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

输出效果:

相关推荐
二王一个今几秒前
Python打包成exe(windows)或者app(mac)
开发语言·python·macos
C嘎嘎嵌入式开发5 小时前
(2)100天python从入门到拿捏
开发语言·python
Stanford_11065 小时前
如何利用Python进行数据分析与可视化的具体操作指南
开发语言·c++·python·微信小程序·微信公众平台·twitter·微信开放平台
Vallelonga6 小时前
Rust 中的数组和数组切片引用
开发语言·rust
Kiri霧6 小时前
Rust模式匹配详解
开发语言·windows·rust
white-persist7 小时前
Python实例方法与Python类的构造方法全解析
开发语言·前端·python·原型模式
千里马-horse7 小时前
Async++ 源码分析8--partitioner.h
开发语言·c++·async++·partitioner
Lucis__8 小时前
再探类&对象——C++入门进阶
开发语言·c++
007php0078 小时前
某大厂跳动面试:计算机网络相关问题解析与总结
java·开发语言·学习·计算机网络·mysql·面试·职场和发展
你的人类朋友8 小时前
先用js快速开发,后续引入ts是否是一个好的实践?
前端·javascript·后端