Full Code Listing Up to the "Consolidate the Drawing Code" Page

Return back to the "Consolidate the Drawing Code" page.

src/main.rs
/* main.rs */

mod coalcar;
mod d51;
mod jack;

use d51::*;

fn main() {
    draw_d51();
    jack::draw_jack();
    coalcar::draw_coalcar();
} // end of main()

fn convert_to_vecvecstring(image_str: &str) -> Vec<Vec<String>> {
    /*
     * This is bad coding - we're *assuming* the &str to be ASCII-sized
     * chars; include a non-ASCII character, like '汉', in your drawing,
     * where a string operation tries to access it, like as the first
     * character of the image's const declaration
     * (pub const D51: &str = r"汉),
     * and watch your program go BOOM!
     */
    //The first character in the string is a newline; lose it.
    let image_str = &image_str[1..image_str.len()];

    // Create new blank vector of vector of Strings.
    let mut outer_vec: Vec<Vec<String>> = Vec::new();

    // This keeps track of which frame/inner vector we're processing.
    let mut inner_vec_num: usize = 0;

    // Create first inner vector in the outer vector.
    outer_vec.push(Vec::new());

    for each_line in image_str.lines() {
        if each_line != "" {
            // Remove single-quote mark at end of each line, if it exists..
            let each_line = if &each_line[each_line.len() - 1..] == "'" {
                each_line[0..each_line.len() - 1].to_string()
            } else {
                each_line.to_string()
            };
            // Then add the string to the current inner vector.
            outer_vec[inner_vec_num].push(each_line);
        } else {
            outer_vec.push(Vec::new());
            inner_vec_num += 1;
        }
    }
    return outer_vec; // We're returning the entire vector of String vectors.
} // end of convert_to_vecvecstring()
src/d51.rs


  
    
    
    Full Code Listing Up to the "Use ncurses to Draw the Images" Page
    
    
  
  
    

Full Code Listing Up to the "Use ncurses to Draw the Images" Page

Return back to the "Use ncurses to Draw the Images" Page

src/main.rs
/* main.rs */

mod coalcar;
mod d51;
mod jack;

use d51::*;

fn main() {
    draw_d51();
    jack::draw_jack();
    coalcar::draw_coalcar();
} // end of main()

fn convert_to_vecvecstring(image_str: &str) -> Vec<Vec<String>> {
    /*
     * This is bad coding - we're *assuming* the &str to be ASCII-sized
     * chars; include a non-ASCII character, like '汉', in your drawing,
     * where a string operation tries to access it, like as the first
     * character of the image's const declaration
     * (pub const D51: &str = r"汉),
     * and watch your program go BOOM!
     */
    //The first character in the string is a newline; lose it.
    let image_str = &image_str[1..image_str.len()];

    // Create new blank vector of vector of Strings.
    let mut outer_vec: Vec<Vec<String>> = Vec::new();

    // This keeps track of which frame/inner vector we're processing.
    let mut inner_vec_num: usize = 0;

    // Create first inner vector in the outer vector.
    outer_vec.push(Vec::new());

    for each_line in image_str.lines() {
        if each_line != "" {
            // Remove single-quote mark at end of each line, if it exists..
            let each_line = if &each_line[each_line.len() - 1..] == "'" {
                each_line[0..each_line.len() - 1].to_string()
            } else {
                each_line.to_string()
            };
            // Then add the string to the current inner vector.
            outer_vec[inner_vec_num].push(each_line);
        } else {
            outer_vec.push(Vec::new());
            inner_vec_num += 1;
        }
    }
    return outer_vec; // We're returning the entire vector of String vectors.
} // end of convert_to_vecvecstring()

Notice that in the "jack.rs" and "coalcar.rs" files I'm using a use crate::*; line, whereas in the "d51.rs" file I'm just putting that path where it's needed within the code. Either method is acceptable, but it's probably best to do things in a consistent manner throughout a project. I've only done it this inconsistent way to demonstrate both methods.

src/d51.rs
/* d51.rs */

pub fn draw_d51() {
    // Convert const into a vector of String vectors.
    let image_vecvecstring = crate::convert_to_vecvecstring(D51);

    for each_inner_vec in image_vecvecstring {
        for each_line in &each_inner_vec {
            println!("{}", each_line);
        }
    }
} // end of draw_d51()

pub const D51: &str = r"
      ====        ________                ___________'
  _D _|  |_______/        \__I_I_____===__|_________|'
   |(_)---  |   H\________/ |   |        =|___ ___|  '
   /     |  |   H  |  |     |   |         ||_| |_||  '
  |      |  |   H  |__--------------------| [___] |  '
  | ________|___H__/__|_____/[][]~\_______|       |  '
  |/ |   |-----------I_____I [][] []  D   |=======|__'
__/ =| o |=-~~\  /~~\  /~~\  /~~\ ____Y___________|__'
 |/-=|___|=    ||    ||    ||    |_____/~\___/       '
  \_/      \O=====O=====O=====O_/      \_/           '

      ====        ________                ___________'
  _D _|  |_______/        \__I_I_____===__|_________|'
   |(_)---  |   H\________/ |   |        =|___ ___|  '
   /     |  |   H  |  |     |   |         ||_| |_||  '
  |      |  |   H  |__--------------------| [___] |  '
  | ________|___H__/__|_____/[][]~\_______|       |  '
  |/ |   |-----------I_____I [][] []  D   |=======|__'
__/ =| o |=-~~\  /~~\  /~~\  /~~\ ____Y___________|__'
 |/-=|___|=O=====O=====O=====O   |_____/~\___/       '
  \_/      \__/  \__/  \__/  \__/      \_/           '

      ====        ________                ___________'
  _D _|  |_______/        \__I_I_____===__|_________|'
   |(_)---  |   H\________/ |   |        =|___ ___|  '
   /     |  |   H  |  |     |   |         ||_| |_||  '
  |      |  |   H  |__--------------------| [___] |  '
  | ________|___H__/__|_____/[][]~\_______|       |  '
  |/ |   |-----------I_____I [][] []  D   |=======|__'
__/ =| o |=-O=====O=====O=====O \ ____Y___________|__'
 |/-=|___|=    ||    ||    ||    |_____/~\___/       '
  \_/      \__/  \__/  \__/  \__/      \_/           '

      ====        ________                ___________'
  _D _|  |_______/        \__I_I_____===__|_________|'
   |(_)---  |   H\________/ |   |        =|___ ___|  '
   /     |  |   H  |  |     |   |         ||_| |_||  '
  |      |  |   H  |__--------------------| [___] |  '
  | ________|___H__/__|_____/[][]~\_______|       |  '
  |/ |   |-----------I_____I [][] []  D   |=======|__'
__/ =| o |=-~O=====O=====O=====O\ ____Y___________|__'
 |/-=|___|=    ||    ||    ||    |_____/~\___/       '
  \_/      \__/  \__/  \__/  \__/      \_/           '

      ====        ________                ___________'
  _D _|  |_______/        \__I_I_____===__|_________|'
   |(_)---  |   H\________/ |   |        =|___ ___|  '
   /     |  |   H  |  |     |   |         ||_| |_||  '
  |      |  |   H  |__--------------------| [___] |  '
  | ________|___H__/__|_____/[][]~\_______|       |  '
  |/ |   |-----------I_____I [][] []  D   |=======|__'
__/ =| o |=-~~\  /~~\  /~~\  /~~\ ____Y___________|__'
 |/-=|___|=   O=====O=====O=====O|_____/~\___/       '
  \_/      \__/  \__/  \__/  \__/      \_/           '

      ====        ________                ___________'
  _D _|  |_______/        \__I_I_____===__|_________|'
   |(_)---  |   H\________/ |   |        =|___ ___|  '
   /     |  |   H  |  |     |   |         ||_| |_||  '
  |      |  |   H  |__--------------------| [___] |  '
  | ________|___H__/__|_____/[][]~\_______|       |  '
  |/ |   |-----------I_____I [][] []  D   |=======|__'
__/ =| o |=-~~\  /~~\  /~~\  /~~\ ____Y___________|__'
 |/-=|___|=    ||    ||    ||    |_____/~\___/       '
  \_/      \_O=====O=====O=====O/      \_/           '
";
src/jack.rs
/* jack.rs */

use crate::*;

pub fn draw_jack() {
    // Convert const into a vector of String vectors.
    let image_vecvecstring = convert_to_vecvecstring(JACK);

    for each_inner_vec in image_vecvecstring {
        for each_line in &each_inner_vec {
            println!("{}", each_line);
        }
    }
} // end of draw_jack()

pub const JACK: &str = r"
 \ 0 /   '
  \|/    '
   |     '
  / \    '
_/   \_  '

         '
 __0__   '
/  |  \  '
  / \    '
 _\ /_   '

         '
   o     '
 /\ /\   '
 |/ \|   '
 _\ /_   '

         '
 __0__   '
/  |  \  '
  / \    '
 _\ /_   '
"; // end of JACK

src/coalcars.rs
/* coalcar.rs */

use crate::convert_to_vecvecstring;

pub fn draw_coalcar() {
    // Convert const into a vector of String vectors.
    let image_vecvecstring = convert_to_vecvecstring(COALCAR);

    for each_inner_vec in image_vecvecstring {
        for each_line in &each_inner_vec {
            println!("{}", each_line);
        }
    }
} // end of draw_coalcar()

pub const COALCAR: &str = r"
    _________________         
   _|                \_____A  
 =|                        |  
 -|                        |  
__|________________________|_ 
|__________________________|_ 
   |_D__D__D_|  |_D__D__D_|   
    \_/   \_/    \_/   \_/ 
"; // end of COALCAR

Return back to the "Consolidate Drawing Code" page.