fn from_file(file: File) -> Percolation {
        // Open the input file
        let reader = BufReader::new(file);
        let mut lines = reader.lines();

        // Read the size of the grid
        let size = match lines.next() {
            Some(v) => match v.unwrap().parse::<usize>() {
                Ok(v) => v,
                Err(e) => panic!("Cannot parse first line as an integer: {:?}", e),
            },
            None => panic!("File is empty"),
        };

        // Create the supporting percolation data structure
        let mut percolation = Percolation::new(size, size);
        for line in lines {
            // Split lines as whitespace separated integers
            let dimension: Vec<usize> = line.unwrap().split_whitespace().map(
                |s| {
                    match s.parse::<usize>() {
                        Ok(v) => v,
                        Err(e) => panic!("Cannot parse component as an integer: {:?}", e),
                    }
                }
            ).collect();
            // Check if we got at least 2
            if dimension.len() < 2 {
                panic!("Cannot parse line {}: need at least two integers")
            }
            // Open site
            percolation.open(dimension[0], dimension[1])
        }

        percolation
    }
/// Read a percolation grid from a file.
///
/// The file should have as the first row the size of the grid (only square grids are supported
/// for the time being). The following lines should contain the row and the column of the site
/// to open, as white space
Read a percolation grid from a file.
The file should have as the first row the size of the grid (only square grids are supported
for the time being). The following lines should contain the row and the column of the site
to open, as white space
Read a percolation grid from a file . The file should have as the first row the size of the grid ( only square grids are supported for the time being ) . The following lines should contain the row and the column of the site to open as white space
fn from_file(file: File) -> Percolation {
        // Open the input file
        let reader = BufReader::new(file);
        let mut lines = reader.lines();
        // Read the size of the grid
        let size = match lines.next() {
            Some(v) => match v.unwrap().parse::<usize>() {
                Ok(v) => v,
                Err(e) => panic!("Cannot parse first line as an integer: {:?}", e),
            },
            None => panic!("File is empty"),
        };
        // Create the supporting percolation data structure
        let mut percolation = Percolation::new(size, size);
        for line in lines {
            // Split lines as whitespace separated integers
            let dimension: Vec<usize> = line.unwrap().split_whitespace().map(
                |s| {
                    match s.parse::<usize>() {
                        Ok(v) => v,
                        Err(e) => panic!("Cannot parse component as an integer: {:?}", e),
                    }
                }
            ).collect();
            // Check if we got at least 2
            if dimension.len() < 2 {
                panic!("Cannot parse line {}: need at least two integers")
            }
            // Open site
            percolation.open(dimension[0], dimension[1])
        }
        percolation
    }
fn from_file ( file : File ) -> Percolation { // Open the input file let reader = BufReader :: new ( file ) ; let mut lines = reader . lines ( ) ; // Read the size of the grid let size = match lines . next ( ) { Some ( v ) => match v . unwrap ( ) . parse :: < usize > ( ) { Ok ( v ) => v , Err ( e ) => panic ! ( " " e ) , } , None => panic ! ( " " ) , } ; // Create the supporting percolation data structure let mut percolation = Percolation :: new ( size , size ) ; for line in lines { // Split lines as whitespace separated integers let dimension : Vec < usize > = line . unwrap ( ) . split_whitespace ( ) . map ( | s | { match s . parse :: < usize > ( ) { Ok ( v ) => v , Err ( e ) => panic ! ( " " e ) , } } ) . collect ( ) ; // Check if we got at least 2 if dimension . len ( ) < 2 { panic ! ( " " ) } // Open site percolation . open ( dimension [ 0 ] , dimension [ 1 ] ) } percolation }
Read a percolation grid from a file.
Read a percolation grid from a file .
file
File
file
File