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

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

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

Disclosure: My content contains affiliate links.

43,227 questions

56,129 answers

573 users

How to fill a 3x3 grid to be a valid Sudoku grid in Rust

1 Answer

0 votes
// To fill a 3x3 grid to be a valid Sudoku grid, you must ensure that each row,
// column, and the 3x3 grid contains the numbers 1 through 9 without repetition.

use rand::seq::SliceRandom;
use rand::rng;

fn fill_sudoku_grid(grid: &mut [[u8; 3]; 3]) {
    let mut numbers: Vec<u8> = (1..=9).collect();
    let mut rng = rng();
    numbers.shuffle(&mut rng);

    let mut index = 0;
    for i in 0..3 {
        for j in 0..3 {
            grid[i][j] = numbers[index];
            index += 1;
        }
    }
}

fn print_grid(grid: &[[u8; 3]; 3]) {
    for row in grid {
        for &num in row {
            print!("{} ", num);
        }
        println!();
    }
}

fn main() {
    let mut grid = [[0u8; 3]; 3];
    fill_sudoku_grid(&mut grid);
    println!("Generated 3x3 Sudoku Grid:");
    print_grid(&grid);
}

   
    
/*
run:
    
Generated 3x3 Sudoku Grid:
6 9 2 
5 7 4 
8 3 1
    
*/

 



answered Jun 1, 2025 by avibootz

Related questions

...