Is it considered bad style to declare multiple "use" statements in Rust?
I am a C++ programmer that recently began trying out Rust. One thing I've noticed as I review Rust code is that in many Rust programs there will be a bunch of use
statements at the top of the program. Coming from C++, it was discouraged to use using namespace std
especially when making header files, but that doesn't seem to be the case in most of the Rust programs I've seen.
So, which of the following trivial examples is considered to be better Rust programming style? Does it change if you're making a binary program vs. a module? And why?
use std::sync::Arc;
use std::sync::Mutex;
use std::thread::Thread;
use std::rand::random;
fn main() {
let mut vec: Vec<char> = (0u8..10).map(|i| i as char).collect();
let mut data = Arc::new(Mutex::new(vec));
for i in 1usize..10 {
let data = data.clone();
let thread = Thread::spawn(move || {
let mut data = match data.lock() {
Ok(guard) => guard,
Err(e) => panic!("{}, was poisoned", e)
};
data.push(random::<char>());
});
}
}
Or this...
fn main() {
let mut vec: Vec<char> = (0u8..10).map(|i| i as char).collect();
let mut data = std::sync::Arc::new(
std::sync::Mutex::new(vec)
);
for i in 1usize..10 {
let data = data.clone();
let thread = std::thread::Thread::spawn(move || {
let mut data = match data.lock() {
Ok(guard) => guard,
Err(e) => panic!("{}, was poisoned", e)
};
data.push(std::rand::random::<char>());
});
}
}