Skip to content

Instantly share code, notes, and snippets.

@jacobbridges
Created November 5, 2019 05:17
Show Gist options
  • Save jacobbridges/9ae36d9d9cdd965348204b127be51048 to your computer and use it in GitHub Desktop.
Save jacobbridges/9ae36d9d9cdd965348204b127be51048 to your computer and use it in GitHub Desktop.
use std::io;
use std::io::Write;
fn main() {
println!("This program draws a rectangle!");
let mut length_input = String::new();
print!("How long should the rectangle be: ");
io::stdout().flush().unwrap();
io::stdin()
.read_line(&mut length_input)
.expect("Failed to get user input!");
let mut height_input = String::new();
print!("How tall should the rectangle be: ");
io::stdout().flush().unwrap();
io::stdin()
.read_line(&mut height_input)
.expect("Failed to get user input!");
let length: i32 = length_input
.trim()
.parse()
.expect("Failed to convert length to a number!");
let height: i32 = height_input
.trim()
.parse()
.expect("Failed to convert height to a number!");
for h in 1..=height {
for l in 1..=length {
print!("{}", get_rectangle_character_at_point(h, l, height, length));
}
print!("\n");
}
}
fn get_rectangle_character_at_point(h: i32, l: i32, total_height: i32, total_length: i32) -> char {
return if h == 1 {
if l == 1 || l == total_length {
'+'
} else {
'-'
}
} else if h == total_height {
if l == 1 || l == total_length {
'+'
} else {
'-'
}
} else if l == 1 || l == total_length {
'|'
} else {
' '
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment