You are on page 1of 2

# Getting Started

## Installation
- install rust `curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh`
(this also installs Cargo)
- install linker `xcode-select --install`

once Rust is installed, here are the available commands:


- `rustup update`
- `rustc --version`
- `rustup doc` - for local documentation

## Hello World

```rust
fn main() {
println!("Hello, world!");
}
```

- create file with extension `.rs`


- compile `rustc main.rs`
- this creates the executable file
- execute `./main`

## Cargo - build system and package manager


Cargo documentation: https://doc.rust-lang.org/cargo/

For more complex projects instead of using `rustc` use Cargo.

- install cargo
- use `cargo --version` if cargo was not installed
- create project with cargo
- `cargo new hello_cargo`
- this generates
- `Cargo.toml`
```rust
[package] - this is a headline
name = "hello_cargo"
version = "0.1.0"
edition = "2021"

[dependencies] - this is a headline


```
- `src` directory with a `main.rs` file inside
- inits git repo and creates `.gitignore`

### Building and Running Cargo Project

- `cargo build`
- `cargo run` or `./target/debug/hello_cargo`
- the result is stored in `target/debug` directory
- `cargo check` - compiles, but doesn't create an executable
- faster than build
- `cargo build --release` - for release
- creates executable with optimizations to run faster, but build and
compilation is long
- the result is stored in `target/release` directory

You might also like