I have the following code:
use std::any::type_name_of_val;
fn main() {
let (a, b, c, d) = (0, 0., '0', "0"); // something many variables
println!("{}", type_name_of_val(&a));
println!("{}", type_name_of_val(&b));
println!("{}", type_name_of_val(&c));
println!("{}", type_name_of_val(&d));
}
I want to use for
and iterate over a tuple (a, b, c, d)
and print the type of the value. I searched, and found rust - Is it possible to iterate over a tuple? - Stack Overflow but it only says tuple can't be iterated and iterate over a same type. I also found Reddit - Dive into anything but there is no solution. How to iterate over a multiple variables with different types in Rust? C++ version is like this:
#include <tuple>
#include <iostream>
#include <boost/core/demangle.hpp>
int main() {
auto const [a, b, c, d] = std::tuple{0, 0., '0', "0"};
std::apply([](auto&&... args) {
((std::cout << boost::core::demangle(typeid(args).name()) << std::endl), ...);
}, std::tuple{a, b, c, d});
}