Because it's a threadpool - so the number of threads is fixed and will be recycled for different tasks (more accurately, multiple tasks will be scheduled to the same thread).
If you want one thread per task, use std::thread::spawn.
Here is the thread spawn code with long process but still same result
Code
use std::process;
use std::thread::{spawn, JoinHandle};
fn main() {
let size = 8;
let mut p: Vec<JoinHandle<()>> = Vec::new();
(0..size).for_each(|i| {
let s = spawn(move || {
println!("{} - {}", i, process::id());
loop {}
});
p.push(s);
});
for pp in p {
pp.join().unwrap();
}
}
It looks like Rust has nothing in the standard library for getting the current thread ID. With the JoinHandle returned from thread::spawn(), one can call JoinHandleExt::as_pthread_t() to get the corresponding pthread_t, but the OS thread ID is not exposed. Outside of the standard library, one can call the function nix::unistd::gettid() from the nix crate to get the OS thread ID.
Thread::id() does not return the thread ID as reported by the OS, nor the handle as reported by libpthread, but instead an opaque ID useful only for distinguishing threads. Currently, it's implemented using an incrementing static variable, which is what OP means by an "index".