tl;dr This worked for me (iTerm2 on Mac)
$ cargo --color always build | less -R 2>&1
When I use
bat) to read the output of
cargo build, the output is not colored, even with
--color always (i.e.
$ cargo --color always build |& lv).
By saving the output to file, I found that
rustc are working correctly -- both emits ANSI escape sequence. However, it seems that
rustc uses codes like
ESC[38;5;12m, which (I heard) is an extension of ANSI and not supported by all softwares.
I found that
less -R can pass through the extended code. So the answer is
$ cargo --color always build |& less -R
And you can also use
$ cargo --color always build |& bat --pager='less -R'
(I see no reason to do that though.)