GithubHelp home page GithubHelp logo

anthrax3 / fomat-macros Goto Github PK

View Code? Open in Web Editor NEW

This project forked from krdln/fomat-macros

0.0 2.0 0.0 27 KB

Alternative syntax for printing macros in Rust

License: MIT License

Rust 100.00%

fomat-macros's Introduction

fomat-macros

documentation, crate

This crate provides alternative syntax for write!, writeln!, print!, println!, epint!, epintln! and format! macros from the Rust standard library. (plus two macros for printing on stderr).

The names of macros in this crate are formed by removing the letter r from their std counterparts: wite!, witeln!, pint!, pintln!, epint!, epintln!, fomat!.

Installation

Add this to your Cargo.toml:

[dependencies]
fomat-macros = "0.2.1"

And import the macros in your .rs file:

#[macro_use] extern crate fomat_macros;

Examples

pintln!("Hello, world!");
pintln!(); // empty line
pintln!("The answer is "(40 + 2)); // parentheses use the Display trait
pintln!([vec![1, 2, 3]] " -- numbers"); // brackets use the Debug trait

As you can see, instead the format string and arguments, we have a list of things to print without any separators. Each thing may be a string literal or an expression in brackets (apart of () and [] there are also braces {}, which may be used for more advanced format specifiers, see the docs).

You can also use if, if let, match and for constructs inside the macro. They use regular Rust syntax, except everything inside the {} blocks will use the list-of-things-to-print syntax again.

let list = vec![1, 2, 3];
let s = fomat!( for x in &list { (x) " :: " } "nil" );
// s == "1 :: 2 :: 3 :: nil"

For loops can also use an optional separator. For details, see the docs.

There's also a shorthand for debugging, which prints both the expression and value. To enable, put = as the first character inside the any kind of brackets.

let list = vec![1, 2, 3];
epintln!([=list]); // prints list = [1, 2, 3]

Why?

What was the motivation to create this crate?

  • More locality โ€“ everything is written in the same order it will be printed. But that might a personal preference.

  • Easier to refactor โ€“ especially when you suddenly want to add a conditional print inside a long format string. Compare:

    let s = fomat!(
        "first line\n"
        if condition() { (foo) }
        "third line\n"
    );
    let s = {
        use ::std::fmt::Write;
        let mut s = "first line\n".to_owned();
        if condition() { write!(s, "{}", foo).unwrap() }
        write!(s, "third line\n").unwrap();
        s
    };
  • Speed! fomat! may be faster than format! (see cargo bench). That's because of optimization of how string literals are handled.

Limitations

The write! and writeln! macros work on everything that has a .write_fmt method. This crate requires also the .write_str method. It works for any io::Write or fmt::Write, but in unlikely circumstances if you're using something custom, you should consult the source.

Is it a templating language?

Kind of, but please don't use it as HTML-templating language for security critical code, as it performs no escaping of special characters.

fomat-macros's People

Contributors

krdln avatar

Watchers

James Cloos avatar  avatar

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.