Set up
Last updated
Last updated
Rust is a general-purpose, statically-typed, functionally-inspired, high-level programming language that specializes in memory safety and speed and features an algebraic type system.
General-purpose — Not limited to a particular application or platform, Rust can be used to build a diverse variety of solutions.
Statically-typed — Data types are known (or computed) and enforced at compile time.
Functionally-inspired — Many features are imported from, or are derivatives of features from FP-paradigm languages like Ocaml and Haskell.
High-level — Rust provides a large degree of abstraction from assembler code.
Memory safety — All safe code (that which does not use the unsafe
keyword) is guaranteed by the compiler to not contain memory safety issues such as dangling pointers, null dereferencing, or accidental memory leaks.
Speed — Rust code compiles directly to machine code with zero or near-zero runtime overhead. It can fairly easily interoperate directly with C code. With the absence of a garbage collector, or really any runtime to speak of, memory is managed by the programmer, albeit within the strict bounds enforced by the compiler’s memory safety features, notwithstanding [unsafe
blocks]().
Algebraic type system — While most modern programming languages include some form of , Rust also includes a fully-featured as well, in the form of a discriminated union ([enum](<https://doc.rust-lang.org/rust-by-example/custom_types/enum.html>)
).
If you don’t want to install anything, you can just use the , and . Otherwise, if you want to set up your own system, just follow , as it’s not worth duplicating the information here.
If you follow the website, you’ll end up with a few different tools installed on your system:
rustup
manages your Rust installation(s): what versions you have installed, for which targets, etc.
rustc
is the Rust compiler. Although you can use it directly, it is cumbersome for large and complex projects.
cargo
will probably be the most-used tool of the three mentioned here during your Rust career. It is an all-in-one project manager, dependency installer, linter, formatter, documentation builder, and build tool. (Technically it delegates out most of those commands to dedicated tools like rustc
, rustdoc
, rustfmt
, and clippy
, but Cargo brings them all together nicely.)
You can check that everything is installed properly by running cargo version
, and you should see something like this:
$ cargo version cargo 1.66.1 (ad779e08b 2023-01-10)
rustup update stable
Let’s get up-and-running as quickly as possible. If you’re familiar with Node.js, Cargo may feel a bit similar.
Create a new project in the directory hello-rust
:
cargo new --bin hello-rust
The --bin
flag means that the new project will create standalone executable binary. The other option is the --lib
flag, which will create a library package, which will not compile into an executable application. If neither flag is specified, creating a binary application is the default.
Let’s quickly make sure that everything is working correctly by building and running the default generated “Hello World” project:
cd hello-rust cargo run
cargo run
will implicitly run cargo build
to build the application if the existing build files are out-of-date.
If everything worked according to plan, you should see something like the following:
Compiling hello-rust v0.1.0 (/.../hello-rust) Finished dev [unoptimized + debuginfo] target(s) in 0.92s Running
target/debug/hello-rust Hello, world!
If everything appears to be working correctly, open up src/main.rs
in your favorite editor.
You can check the latest stable version number on , and you can upgrade the version currently installed on your computer with this command: