/* 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
/* 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.