y compilar tu primer programa
1.2 Cómo ejecutar
Let’s make a new folder named Rust Examples for storing all our Rust code files. We’ll add the example1.rs file to it containing the same code shown earlier in Listing 1.1. Let’s open this file in VS Code by accessing File · Open File. You should be aware that the editor will not auto-save your program be sure you save manually after making changes.
To compile a program, we’ll write commands in the terminal. The terminal may be made visible via the View menu by selecting Terminal. Enter following command to compile the Rust code file.
c:\ Rust Examples> rustc example.rsProgram execution will take a few seconds, and if no errors arise, the program will return the control to the terminal without any additional messages. You may notice that it has generated executable file corresponding to the code file in the Explorer pane. Now, to run the executable file, enter the following command.
c:\ Rust Examples> ./example.rsThis approach is suitable for simple programs contained within a single file. However, in real-world scenarios, programs are usually complex, often involving multiple files and dependencies. In such cases, utilizing the Cargo utility is the ideal solution.
Cargo helps you manage Rust projects by efficiently organizing code, dependencies, and build processes. This tool is especially useful when working with multiple files, libraries, and external crates, by making the development process more streamlined and scalable. To create a project using Cargo, enter the following command:
c:\> cargo new Rust_ExamplesThis command sets up the directory structure for a new project named Rust_Examples inside the specified folder. The directory contains many files. The Cargo.toml file is the central configuration file for a Rust project managed by Cargo. This file contains metadata and setting that define the project’s dependencies, build settings, and other relevant information. The .gitignore file contains the files or directories generated during the build process, temporary files, or sensitive data that should not be included in version control. The target directory contains build artifacts generated by Cargo, including compiled binaries, intermediate files, and dependencies. This directory organizes these files by building profiles (e.g., debug, release) and is automatically created when you build a Rust project.
Finally, the src folder contains all the Rust code source files with .rs extensions. By default, it contains a hello.rs, which is an autogenerated file and contains code similar to Listing 1.1. The cargo run command compiles your Rust project and immediately executes the resulting binary in one step. This command simplifies the development workflow by combining building and running. For instance, to execute our created projects and the binary file residing inside it, enter the following command:
c:\Rust_Examples> cargo runMaske sure that the project Rust_Examples is in your current working directory. At this point, we’ve covered the basics of executing Rust programs.
The cargo build command is used in situations where you only want to build the program executable and are not interested in the resulting output of your program. This command simple compiles the Rust project into an executable or library, depending on the project configuration. By default, Cargo builds the project in debug mode. The resulting binaries are stored in the target/debug directory. This command is commonly used during development to quickly compile and test changes.
When building for production
Back to top