Compile and Run
Rust is an ahead-of-time compiled language where a crate compiles to a true binary, not an intermediate language requiring a virtual machine for execution. Rust is not a managed language. Once compiled, Rust binaries can probably execute anywhere else, even where Rust is not installed.
As stated earlier, rustc is the Rust compiler and is included in the Rust toolchain. It is installed by default with the remainder of the toolchain. rustc can build different types of binaries, depending on the platform. For Linux, the binary format is Linkable Format (ELF file). rustc can also create portable executables (PEs) when building a binary for the Windows platform.
Here, the rustc tool compiles the “Hello, World” crate to an executable binary:
rustc hello.rs
When the executable crate is compiled, two files are generated:
cratename.exe: This is the executable binary.
cratename.pdb: This PDB (program database) contains metadata about the binary, such as symbolic names and source line information. Debuggers, such as GDB, read this PDB file to present user-friendly diagnostics information to developers.
From the hello.rs source file, rustc creates the hello.exe and hello.pdb files.
The rustc compiler is talkative, providing verbose warnings and error messages. References may also be provided for more detail. Here is the compiler error message displayed when the println! macro is improperly used without the obligatory exclamation point:
error[E0423]: expected function, found macro `print` --> hello.rs:2:2 | 2 | println("Hello, world!"); | ^^^^^ not a function | help: use `!` to invoke the macro | 2 | print!("Hello, world!"); | + error: aborting due to previous error For more information about this error, try `rustc --explain E0423`.
Despite the talkativeness of the Rust compiler, it is simply impractical at times to display all relevant error information during compilation. When this occurs, an error identifier is provided. Using the rustc --explain erroridentifier command, you can display additional error information. The additional information entails a detailed explanation of the error, suggestions on correcting the problem, sample code, and more.