Step 1: Install Rust
On Windows, the standard way to install Rust is using rustup, a tool that manages Rust versions and associated tools.
Download the Installer:
Go to the official Rust website:
https://www.rust-lang.org/tools/install Download rustup-init.exe (choose the 64-bit version if your computer is modern).
Run the Installer:
Double-click
rustup-init.exe.Crucial Note: The installer may ask you to install Visual Studio C++ Build Tools. If it does, follow the prompts to install them. Rust needs these Microsoft tools to link your programs correctly on Windows.
When prompted with installation options (1, 2, or 3), type 1 (Proceed with installation) and press Enter.
Verify Installation:
Once the installation finishes, close your current command prompt/terminal and open a new one (Command Prompt or PowerShell).
Type the following to check if Rust is installed:
PowerShellrustc --versionIf you see something like
rustc 1.75.0 (timestamp), you are ready to go!
Step 2: Write the "Hello, World!" Program
Rust files end with the .rs extension.
Create a folder for your project (e.g.,
C:\RustProjects).Open a text editor (Notepad, Notepad++, or VS Code).
Type the following code exactly as it appears:
fn main() {
println!("Hello, world!");
}
Save the file as main.rs inside your project folder.
Explanation of the code:
fn main() { ... }: This defines the main function. It is the entry point where your program begins executing.
println!("..."): This prints text to the console. The
!indicates that this is a macro, not a normal function (a key distinction in Rust).;: Almost all lines in Rust must end with a semicolon.
Step 3: Compile and Execute
Now you need to turn your text file into an executable program (.exe) that Windows can run.
Open your terminal (Command Prompt or PowerShell).
Navigate to the folder where you saved the file:
PowerShellcd C:\RustProjectsCompile the code using the Rust compiler (
rustc):PowerShellrustc main.rs(If successful, nothing will happen in the terminal, but a
main.exeandmain.pdbfile will appear in your folder.)Execute the program:
PowerShell.\main.exe
Expected Output:
Hello, world!
Summary of Commands
| Goal | Command |
| Check Version | rustc --version |
| Compile | rustc filename.rs |
| Run (Windows) | .\filename.exe |

No comments:
Post a Comment
Note: only a member of this blog may post a comment.