This post was last edited by nemon on 2024-8-8 18:20
Last time ( https://en.eeworld.com/bbs/thread-1289538-1-1.html ) I explained what BF is. This time, let’s see how to run it without errors using rust.
Here's a little aside. Use this to install rust under Windows:
When installing the rust environment this time, the network was not stable, so I clicked pause during the installation of visualStudio.
It does not affect the installation of rust at all, but when cargo builds, msvc cannot be found.
When I started the Rust installation again, it always prompted that an installation was not completed when installing Visual Studio, but there was no other place to operate.
Just ran this:
This will display the paused installation task, and then continue the installation, and rust can be used.
By the way, an editor is included. Remember to install the two extensions rust-analyzer and Native Debug——
I have strayed off topic again, let’s get back to the implementation of BF in rust.
In fact, it is also very simple. Basically, just follow the algorithm in the previous article and rewrite it in rust:
fn main() {
let s_prog = String::from("++++++++[>+++++++++<-]>.>>++++++++++[<++++++++++>-]<+.+++++++..+++.>>++++[<+++++++++++>-]<.------------.<<<+++[>+++++<-]>.>.+++.------.--------.>+.");
let length = s_prog.len();
let mut i = 0;
let mut memory: [u8; 32] = [0; 32];
let mut position_in_memory = 0;
let mut call_stack: [u32; 32] = [0; 32];
let mut position_in_stack = 0;
while i < length {
let mut c = &s_prog[i..(i + 1)];
match c {
">" => {
position_in_memory += 1;
}
"<" => {
position_in_memory -= 1;
}
"+" => {
memory[position_in_memory] = memory[position_in_memory] + 1;
}
"-" => {
memory[position_in_memory] -= 1;
}
"." => {
let _ch = std::char::from_u32(memory[position_in_memory] as u32).unwrap();
print!("{}", _ch);
}
"," => {
let mut input = String::new();
let _ = std::io::stdin().read_line(&mut input).expect("无法读取输入");
let char_input = input.chars().next().expect("没有输入字符");
memory[position_in_memory] = char_input as u8 ;
}
"[" => {
if memory[position_in_memory] == 0 {
position_in_stack += 1;
call_stack[position_in_stack] = i as u32;
let mut count = 1;
while count > 0 {
i += 1;
c = &s_prog[i..(i + 1)];
match c {
"[" => {
count += 1;
}
"]" => {
count -= 1;
}
&_ => count = count,
}
}
} else {
position_in_stack += 1;
call_stack[position_in_stack] = i as u32;
}
}
"]" => {
if memory[position_in_memory] != 0 {
i = call_stack[position_in_stack] as usize;
} else {
position_in_stack -= 1;
}
}
_ => println!("非法字符 {}", c),
}
i += 1;
}
}
After cargo build and cargo run, the effect is exactly the same as Python, outputting "Hello World!"
It can be seen that the logic is completely copied from the previous py code, and the syntax is very similar to c. The only big difference is that in rust, if you want to change the value of a variable, you must add mut when declaring it.
Affected by this, the storage unit array and stack array, as well as the program pointer, storage pointer and stack pointer have all undergone this transformation.
(I suddenly thought that I should use c to write the first part, which would be more obvious.)
The rewrite ends here. In the next article, we will demonstrate how to obtain command line parameters and use functions in Rust.