1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*!
# Concat-With

While the `concat!` macro in `std` is useful to create a static string slice (`&'static str`) from literals, it cannot set a separator for those. This crate therefore provides another `concat!` macro to deal with that situation.

```rust
#[macro_use] extern crate concat_with;

assert_eq!("test10btrue", concat!("test", 10, 'b', true));
assert_eq!("test, 10, b, true", concat!(with ", ", "test", 10, 'b', true));
assert_eq!("test\n10\nb\ntrue", concat_line!("test", 10, 'b', true));
```
*/

/// Concatenates literals into a static string slice.
///
/// This macro is like the `concat!` macro in `std`, but can also be used to concatenate literals separated by a specific literal.
///
/// ```rust
/// #[macro_use] extern crate concat_with;
///
/// assert_eq!("test10btrue", concat!("test", 10, 'b', true));
/// assert_eq!("test, 10, b, true", concat!(with ", ", "test", 10, 'b', true));
/// assert_eq!("test\n10\nb\ntrue", concat_line!("test", 10, 'b', true));
/// ```
#[macro_export]
macro_rules! concat {
    ($(,)*) => {
        ""
    };
    ( $e:expr $(,)*) => {
        ::std::concat!($e)
    };
    ( $e:expr $(, $es:expr)+ $(,)*) => {
        ::std::concat!( $e $(, $es)+ )
    };
    (with $w:expr $(,)*) => {
        ""
    };
    (with $w:expr, $e:expr $(,)*) => {
        ::std::concat!($e)
    };
    (with $w:expr, $e:expr $(, $es:expr)+ $(,)*) => {
        ::std::concat!( $e $(, $w, $es)+ )
    };
}

/// Concatenates literals into a static string slice separated by a line break, `\n`.
///
/// ```rust
/// #[macro_use] extern crate concat_with;
///
/// assert_eq!("test\n10\nb\ntrue", concat_line!("test", 10, 'b', true));
/// ```
#[macro_export]
macro_rules! concat_line {
    ($($e:expr),* $(,)*) => {
        $crate::concat!(with "\n" $(, $e)*)
    };
}