Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,844 questions

51,765 answers

573 users

How to split a string into chunks of two characters each in Rust

1 Answer

0 votes
fn split_string_into_chunks(s: &str, chunk_size: usize) -> Vec<String> {
    let mut chunks = Vec::new();
    let length = s.len();

    for i in (0..length).step_by(chunk_size) {
        // Extract the substring and push it to the chunks vector
        let end = usize::min(i + chunk_size, length);
        chunks.push(s[i..end].to_string());
    }

    chunks
}

fn main() {
    let s = "abcdefghijk";
    let chunk_size = 2;

    let chunks = split_string_into_chunks(s, chunk_size);

    println!("Chunks of two characters:");
    for chunk in chunks {
        println!("{}", chunk);
    }
}


      
/*
run:

Chunks of two characters:
ab
cd
ef
gh
ij
k
     
*/

 



answered Mar 30, 2025 by avibootz
...