| 1 | # fomat-macros |
| 2 | |
| 3 | [**documentation**](https://docs.rs/fomat-macros), |
| 4 | [**crate**](https://crates.io/crates/fomat-macros) |
| 5 | |
| 6 | This crate provides alternative syntax for |
| 7 | `write!`, `writeln!`, `print!`, `println!`, `eprint!`, `eprintln!` and `format!` macros |
| 8 | from the [Rust](https://www.rust-lang.org/) standard library. |
| 9 | |
| 10 | The names of macros in this crate |
| 11 | are formed by removing the letter `r` from their `std` counterparts: |
| 12 | `wite!`, `witeln!`, `pint!`, `pintln!`, |
| 13 | `epint!`, `epintln!`, `fomat!`. |
| 14 | |
| 15 | ## Installation |
| 16 | |
| 17 | Add this to your `Cargo.toml`: |
| 18 | |
| 19 | ```toml |
| 20 | [dependencies] |
| 21 | fomat-macros = "0.3.2" |
| 22 | ``` |
| 23 | |
| 24 | And `use` the macros in your `.rs` file, eg.: |
| 25 | |
| 26 | ```rust |
| 27 | use fomat_macros::pintln; |
| 28 | ``` |
| 29 | |
| 30 | This version requires Rust 1.30. For support for older versions, |
| 31 | [see version 0.2.1](https://github.com/krdln/fomat-macros/tree/v0.2.1). |
| 32 | |
| 33 | ## Examples |
| 34 | |
| 35 | ```rust |
| 36 | pintln!("Hello, world!"); |
| 37 | pintln!(); // empty line |
| 38 | pintln!("The answer is "(40 + 2)); // parentheses use the Display trait |
| 39 | pintln!([vec![1, 2, 3]] " -- numbers"); // brackets use the Debug trait |
| 40 | ``` |
| 41 | |
| 42 | As you can see, instead the format string and arguments, |
| 43 | we have a list of *things* to print |
| 44 | without any separators. |
| 45 | Each *thing* may be a string literal or an expression in brackets |
| 46 | (apart of `()` and `[]` there are also braces `{}`, which may |
| 47 | be used for more advanced format specifiers, see the docs). |
| 48 | |
| 49 | You can also use `if`, `if let`, `match` and `for` constructs |
| 50 | inside the macro. They use regular Rust syntax, except |
| 51 | everything inside the `{}` blocks will use the |
| 52 | list-of-things-to-print syntax again. |
| 53 | |
| 54 | ```rust |
| 55 | let list = vec![1, 2, 3]; |
| 56 | let s = fomat!( for x in &list { (x) " :: " } "nil" ); |
| 57 | // s == "1 :: 2 :: 3 :: nil" |
| 58 | ``` |
| 59 | |
| 60 | For loops can also use an optional separator. |
| 61 | [For details, see the docs.](https://docs.rs/fomat-macros/0.2/fomat_macros/#for-loops) |
| 62 | |
| 63 | There's also a shorthand for debugging, which prints both |
| 64 | the expression and value. To enable, put `=` as the first |
| 65 | character inside the any kind of brackets. |
| 66 | |
| 67 | ```rust |
| 68 | let list = vec![1, 2, 3]; |
| 69 | epintln!([=list]); // prints list = [1, 2, 3] |
| 70 | ``` |
| 71 | |
| 72 | ## Why? |
| 73 | |
| 74 | What was the motivation to create this crate? |
| 75 | |
| 76 | * More locality – everything is written in the same order |
| 77 | it will be printed. But that might a personal preference. |
| 78 | * Easier to refactor – especially when you suddenly want |
| 79 | to add a conditional print inside a long format string. Compare: |
| 80 | |
| 81 | ```rust |
| 82 | let s = fomat!( |
| 83 | "first line\n" |
| 84 | if condition() { (foo) } |
| 85 | "third line\n" |
| 86 | ); |
| 87 | ``` |
| 88 | |
| 89 | ```rust |
| 90 | let s = { |
| 91 | use ::std::fmt::Write; |
| 92 | let mut s = "first line\n".to_owned(); |
| 93 | if condition() { write!(s, "{}", foo).unwrap() } |
| 94 | write!(s, "third line\n").unwrap(); |
| 95 | s |
| 96 | }; |
| 97 | ``` |
| 98 | |
| 99 | * Speed! `fomat!` may be faster than `format!` |
| 100 | (see `cargo bench`). That's because there's |
| 101 | just one virtual call for single invocation |
| 102 | of `fomat!` instead of one per each argument |
| 103 | in `std::format!`. |
| 104 | |
| 105 | ## Limitations |
| 106 | |
| 107 | The `write!` and `writeln!` macros work on everything that |
| 108 | has a `.write_fmt` method. This crate requires also |
| 109 | the `.write_str` method. It works for any `io::Write` or |
| 110 | `fmt::Write`, but in unlikely circumstances if you're using something |
| 111 | custom, you should consult the source. |
| 112 | |
| 113 | ## Is it a templating language? |
| 114 | |
| 115 | Kind of, but please don't use it as HTML-templating language |
| 116 | for security critical code, as it performs no escaping |
| 117 | of special characters. |
| 118 |