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

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

安装依赖:

cargo add ansi_term

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

[dependencies]
ansi_term = "0.12"

 使用ansi_term,我们可以很容易地在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:"));
}

输出效果:

相关推荐

  1. 【QT】如何将printf打印输出至窗体

    2024-04-11 23:42:03       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-11 23:42:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-11 23:42:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-11 23:42:03       87 阅读
  4. Python语言-面向对象

    2024-04-11 23:42:03       96 阅读

热门阅读

  1. [树莓派]树莓派Raspbian系统安装tesseract-ocr实现OCR

    2024-04-11 23:42:03       37 阅读
  2. 基于STM32四轴飞行器方案设计 1

    2024-04-11 23:42:03       40 阅读
  3. 每日一题 第八十八期 洛谷 滑动窗口

    2024-04-11 23:42:03       43 阅读
  4. leetcode26--删除有序数组中的重复项

    2024-04-11 23:42:03       31 阅读
  5. IntersectionObserver实现图片懒加载

    2024-04-11 23:42:03       38 阅读
  6. 【前端】CSS常用选择器总结

    2024-04-11 23:42:03       37 阅读
  7. POSTGRESQL——存储过程调试

    2024-04-11 23:42:03       41 阅读