Process read-only string slices in parallel



examples/threads/process-string-slices/src/main.rs
use std::sync::Arc;
use std::thread;

macro_rules! prt {
    ($text: expr, $var: expr) => {
        prt!($text, $var, $var)
    };
    ($text: expr, $var: expr, $show: expr) => {
        println!("{:11} {:p} {:?} {:?}", $text, &$var, $var.as_ptr(), $show);
    };
}

fn main() {
    let text = Arc::new(String::from("The black cat climbed the green tree"));
    let parts = 3;

    println!("len: {} parts: {}", text.len(), parts);

    prt!("Before:", text);

    let mut handles = vec![];

    let size = text.len() / parts;
    for part in 0..parts {
        let start = part * size;
        let end = if start + size + size <= text.len() {
            start + size
        } else {
            text.len()
        };
        //println!("{start:3}-{end:3} {}", &text[start..end]);

        handles.push(thread::spawn({
            let text = text.clone();
            move || {
                prt!(
                    format!("{:?}", thread::current().id()),
                    text,
                    &text[start..end]
                );
            }
        }));
    }
    prt!("Started:", text);

    for handle in handles {
        handle.join().unwrap();
    }

    prt!("After:", text);
}

len: 36 parts: 3
Before:     0x7fff1ba74ea8 0x58b13bed0ae0 "The black cat climbed the green tree"
Started:    0x7fff1ba74ea8 0x58b13bed0ae0 "The black cat climbed the green tree"
ThreadId(2) 0x75d6e05ffb70 0x58b13bed0ae0 "The black ca"
ThreadId(3) 0x75d6e01ffb70 0x58b13bed0ae0 "t climbed th"
ThreadId(4) 0x75d6dfdffb70 0x58b13bed0ae0 "e green tree"
After:      0x7fff1ba74ea8 0x58b13bed0ae0 "The black cat climbed the green tree"