Giter Club home page Giter Club logo

icecream-rs's Introduction

icecream-rs

let x = vec![1, 2, 3];

// regular print debugging...
println!("x = {:?}", x);       // x = [1, 2, 3]

// icecream print debugging.
ic!(x);                        // main.rs:8 ❯ x = [1, 2, 3]

Print debugging with inspection for Rust. Inspired by icecream for Python.

I tend to do a lot of print debugging when writing Rust. icecream provides the ic!() and ice!() macros to make print debugging more convenient by:

  1. Using thestd::fmt::Debug formatter, by default.
  2. Displaying helpful information like line number, calling function, module name, and file name.

Debugging with ic!()

// src/example.rs
#[macro_use]
extern crate icecream;

mod a_module {
    fn some_function() {
        let x = Some(99);
        ic!(x);
    }
}

Basic usage

Plain

ic!() prints the filename and line number.

example.rs:8 ❯

Matching on an identifier

ic!(x) prints the name of the variable and its value.

example.rs:9 ❯ x = Some(99)

Matching on an expression

ic!(x.unwrap() + 1) evaluates the inner expression and prints the resulting value.

example.rs:10 ❯ x.unwrap() + 1 = 100

Printing more information

ice!() prints a longer output that includes the calling module and function.

example.rs::a_module::some_function:11 ❯

Features

Returns its argument

ic!() returns its argument, so it can be inserted into any expression.

let x = Some(99);
assert_eq!(ic!(x), Some(99))   // main.rs:8 ❯ x = Some(99)

Configurable

You can also configure the characters used for symbols in the print output.

// main.rs
#[macro_use]
extern crate icecream;

fn main() {
    icecream::set_equals_symbol(" -> ");
    let x = vec![1, 2, 3];                // main.rs:7 ❯ x -> [1, 2, 3]
    ic!(x);
}

Tests

Tests must be run single-threaded with the --nocapture flag due to a race condition when printing to stdout.

RUST_TEST_THREADS=1 cargo test -- --nocapture

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.