Installing Rust and your first cargo project
Rust is a compiled language designed for writing fast and safe programs: the compiler catches memory errors at compile time that in other languages only show up when the program is already in production. This course assumes you have programmed before in some other language (Python, JavaScript, Java, C...), so we will go straight to what makes Rust special. We start with the practical part: installing the tools and running a program.
Installing with rustup
The official way to install Rust is rustup, a manager that downloads the compiler (rustc) and the project manager (cargo), and lets you update or switch versions. On Windows, download the installer from rust-lang.org/tools/install; on Linux and macOS run the command shown on that same page. On Windows you also need the Visual Studio C++ build tools (the installer tells you). When it finishes, open a new terminal and check the installation with the commands below. To update later, just run rustup update.
# check that everything is installed
rustc --version
cargo --version
rustup updateIf the terminal answers with version numbers, you are ready. If it says the command does not exist, it is almost always because the terminal was opened before installing: close it and open a new one so it reads the updated PATH.
A hand-made program with rustc
Before using cargo, let us see the bare minimum. A Rust program starts in the main function. The println! line ends with an exclamation mark because it is a macro, not a normal function; for now just remember that it prints text and adds a newline. Save this as hello.rs and compile it with rustc hello.rs; it produces an executable you run directly.
fn main() {
// println! is a macro: it prints text plus a newline
println!("Hello, Rust!");
}Cargo: the center of the workflow
In real projects you almost never call rustc by hand. Cargo creates the structure, builds, runs, downloads dependencies and runs tests. cargo new name creates a folder with a Cargo.toml file (name, version, edition and dependencies) and src/main.rs with a Hello world already written. The 2024 edition is the newest one and cargo new sets it by default.
cargo new hello_cargo
cd hello_cargo
cargo run # compile + run (debug mode)
cargo check # only checks errors, much faster
cargo build --release # optimized binary in target/release- cargo run: builds (if needed) and runs.
- cargo check: verifies the code compiles without producing the executable; use it while you write.
- cargo build: produces the binary in target/debug.
- cargo build --release: produces the optimized binary in target/release (slower to build, faster to run).
Common mistake: forgetting that target/ is a generated folder. You do not edit it or commit it to git (cargo new already creates a .gitignore that excludes it). Another frequent mistake is measuring speed with cargo run: debug mode is slow on purpose; to measure, use --release.
Reading the compiler messages
Rust error messages are famous for being clear: they show the file, the line, what was expected and often how to fix it. Get used to reading them from the top and fixing the first error first; the next ones are often a consequence of it. Write your program with a deliberate typo (for example printn! instead of println!) to see how the compiler answers.
fn main() {
let name = "Ada";
// {} inserts a value; {name} uses the variable directly
println!("Hello, {}!", name);
println!("Hello again, {name}!");
}理解度チェック
問題 1 / 1Which command checks for compile errors without producing the executable?
✎ マイノート
このブラウザにのみ保存されます。ログインするとアカウントに保存できます。0/5000 文字