Basics of Rust
Descrition del contenete del págine
Conversion de old BASIC-programas a Rust por aprender lu elementari de ti-ci lingue.
Etiquettes:
3D Plot
/*
3D Plot
Original version in BASIC:
Creative Computing (Morristown, New Jersey, USA), ca. 1980.
This version in Rust:
Copyright (c) 2024, Marcos Cruz (programandala.net)
SPDX-License-Identifier: Fair
Written on 2024-12-26/27.
Last modified: 20241227T1235+0100.
*/
use std::io;
use std::io::Write;
const SPACE: char = ' ';
const DOT: char = '*';
const WIDTH: usize = 56;
fn move_cursor_home() {
print!("\x1B[H")
}
fn erase_screen() {
print!("\x1B[2J")
}
fn clear_screen() {
erase_screen();
move_cursor_home()
}
fn press_enter(prompt: &str) {
print!("{prompt}");
io::stdout().flush().unwrap(); // print pending text
let mut input = String::new();
let _ = io::stdin().read_line(&mut input);
}
// Display the credits and wait for a keypress.
//
fn print_credits() {
println!("3D Plot\n");
println!("Original version in BASIC:");
println!(" Creative computing (Morristown, New Jersey, USA), ca. 1980.\n");
println!("This version in Rust:");
println!(" Copyright (c) 2024, Marcos Cruz (programandala.net)");
println!(" SPDX-License-Identifier: Fair\n");
press_enter("Press Enter to start the program. ")
}
fn a(z: f64) -> f64 {
return 30.0 * f64::exp(-z * z / 100.0)
}
fn draw() {
let mut l: isize;
let mut z: isize;
let mut y1: isize;
let mut line : [char; WIDTH];
let mut x: f64 = -30.0;
while x <= 30.0 {
line = [SPACE; WIDTH];
l = 0;
y1 = 5 * (f64::sqrt(900.0 - x * x) / 5.0) as isize;
let mut y = y1;
while y >= -y1 {
z = (25.0 + a(f64::sqrt(x * x + (y * y) as f64)) - 0.7 * (y as f64)) as isize;
if z > l {
l = z;
line[z as usize] = DOT
}
y += -5
} // y loop
for pos in 0 .. WIDTH {
print!("{}", line[pos])
}
println!();
x += 1.5
} // x loop
}
fn main() {
clear_screen();
print_credits();
clear_screen();
draw()
}
Diamond
/*
Diamond
Original version in BASIC:
Example included in Vintage BASIC 1.0.3.
http://www.vintage-basic.net
This version in Rust:
Copyright (c) 2024, Marcos Cruz (programandala.net)
SPDX-License-Identifier: Fair
Written on 2024-12-26.
Last modified: 20241226T2041+0100.
*/
const LINES:isize = 17;
fn main() {
for i in 1 ..= LINES / 2 + 1 {
for _ in 1 ..= (LINES + 1) / 2 - i + 1 {
print!(" ")
}
for _ in 1 ..= i * 2 - 1 {
print!("*")
}
println!()
}
for i in 1 ..= LINES / 2 {
for _ in 1 ..= i + 1 {
print!(" ")
}
for _ in 1 ..= ((LINES + 1) / 2 - i) * 2 - 1 {
print!("*")
}
println!()
}
}
Sine Wave
/*
Sine Wave
Original version in BASIC:
Creative Computing (Morristown, New Jersey, USA), ca. 1980.
This version in Rust:
Copyright (c) 2024, Marcos Cruz (programandala.net)
SPDX-License-Identifier: Fair
Written in 2024-12-27/28.
Last modified: 20241228T2054+0100.
*/
use std::io;
use std::io::Write;
fn move_cursor_home() {
print!("\x1B[H")
}
fn erase_screen() {
print!("\x1B[2J")
}
fn clear_screen() {
erase_screen();
move_cursor_home();
}
fn read_string(prompt: &str) -> String {
print!("{prompt}");
io::stdout().flush().unwrap(); // print pending text
let mut input = String::new();
io::stdin().read_line(&mut input).expect("");
return input;
}
// Display the credits and wait for a keypress.
//
fn print_credits() {
println!("Sine Wave\n");
println!("Original version in BASIC:");
println!(" Creative Computing (Morristown, New Jersey, USA), ca. 1980.\n");
println!("This version in Rust:");
println!(" Copyright (c) 2024, Marcos Cruz (programandala.net)");
println!(" SPDX-License-Identifier: Fair\n");
let _ = read_string("Press Enter to start the program. ");
}
fn draw() {
let mut even = false;
let mut angle = 0.0;
const ORDER: [&str; 2] = ["first", "second"];
let mut word: [String; 2] = ["".into(), "".into()];
for n in 0 ..= 1 {
while word[n].is_empty() {
let prompt = format!("Enter the {} word: ", ORDER[n]);
word[n] = read_string(&prompt).trim().to_string();
}
}
while angle <= 40.0 {
let margin = 26 + f64::floor(25.0 * f64::sin(angle)) as isize;
for _ in 0 .. margin {
print!(" ");
}
println!("{}", word[even as usize]);
even = ! even;
angle += 0.25;
}
}
fn main() {
clear_screen();
print_credits();
clear_screen();
draw();
}
