R1: Cargo Flashcards

1
Q

What is Cargo?

A

Cargo is Rust’s build system and package manager. It can: build your code, download the libraries your code depends on, and build those libraries.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Cmd: cargo new <project name>

A

We can create a project. You’ll see that Cargo has generated two files and one directory for us: a Cargo.toml file and a src directory with a main.rs file inside.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Cmd: cargo new --vcs=git hello_cargo

A

Same as cargo new <project name> but overrides current git config files.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the Cargo.toml contain?

A

This file is in the TOML (Tom’s Obvious, Minimal Language) format, which is Cargo’s configuration format.

Example:

[package]
name = “hello_cargo”
version = “0.1.0”
edition = “2021”

See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is [package] in Cargo.toml?

A

The first line, [package], is a section heading that indicates that the following statements are configuring a package.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is [dependencies] in Cargo.toml?

A

Is the start of a section for you to list any of your project’s dependencies. In Rust, packages of code are referred to as crates.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Cmd: cargo build

A

We can build a project

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Cmd: cargo run

A

We can build and run a project in one step

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Cmd: cargo check

A

We can build a project without producing a binary to check for errors using

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

target/debug directory

A

Instead of saving the result of the build in the same directory as our code, Cargo stores it in the target/debug directory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Cmd: cargo build --release?

A

When your project is finally ready for release, you can use cargo build –release to compile it with optimizations. This command will create an executable in target/release instead of target/debug.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Cmd: cargo build --release?

A

When your project is finally ready for release, you can use cargo build –release to compile it with optimizations. This command will create an executable in target/release instead of target/debug.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

module system

A

Rust has a number of features that allow you to manage your code’s organization, including which details are exposed, which details are private, and what names are in each scope in your programs.

These features include:

  • Packages: A Cargo feature that lets you build, test, and share crates
  • Crates: A tree of modules that produces a library or executable
  • Modules and use: Let you control the organization, scope, and privacy of paths
  • Paths: A way of naming an item, such as a struct, function, or module
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

crate

A

A crate is the smallest amount of code that the Rust compiler considers at a time. A crate can come in one of two forms:
- Binary crates: are programs you can compile to an executable that you can run, such as a command-line program or a server.
- Library crates don’t have a main function, and they don’t compile to an executable. Instead, they define functionality intended to be shared with multiple projects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

crate root

A

The crate root is a source file that the Rust compiler starts from and makes up the root module of your crate (usually src/lib.rs for a library crate or src/main.rs for a binary crate).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

package

A

A package is a bundle of one or more crates that provides a set of functionality. A package contains a Cargo.toml file that describes how to build those crates.

17
Q

Declaring submodules

A

Declaring modules: In the crate root file, you can declare new modules; say, you declare a “garden” module with mod garden;.

Once a module is part of your crate, you can refer to code in that module from anywhere else in that same crate, as long as the privacy rules allow, using the path to the code. For example, an Asparagus type in the garden vegetables module would be found at crate::garden::vegetables::Asparagus.

18
Q

Private vs public modules

A

Code within a module is private from its parent modules by default. To make a module public, declare it with pub mod instead of mod.

19
Q

use keyword

A

The use keyword: Within a scope, the use keyword creates shortcuts to items to reduce repetition of long paths. In any scope that can refer to crate::garden::vegetables::Asparagus, you can create a shortcut with use crate::garden::vegetables::Asparagus; and from then on you only need to write Asparagus to make use of that type in the scope.

20
Q

backyard

A

Here we create a binary crate named backyard that illustrates these rules. The crate’s directory, also named backyard, contains these files and directories:

backyard
├── Cargo.lock
├── Cargo.toml
└── src
    ├── garden
    │   └── vegetables.rs
    ├── garden.rs
    └── main.rs
21
Q

Example of modules use

A

``` src/main.rs
use crate::garden::vegetables::Asparagus;

pub mod garden;

fn main() {
let plant = Asparagus {};
println!(“I’m growing {:?}!”, plant);
}
~~~

``` src/garden.rs
pub mod vegetables;
~~~

```src/garden/vegetables.rs
#[derive(Debug)]
pub struct Asparagus {}
~~~

22
Q

Grouping Related Code in Modules

A

Examples of fs tree:
~~~
crate
└── front_of_house
├── hosting
│ ├── add_to_waitlist
│ └── seat_at_table
└── serving
├── take_order
├── serve_order
└── take_payment

Examples of module:

mod front_of_house {
mod hosting {
fn add_to_waitlist() {}

    fn seat_at_table() {}
}

mod serving {
    fn take_order() {}

    fn serve_order() {}

    fn take_payment() {}
} } ~~~
23
Q

absolute path

A
  • An absolute path: is the full path starting from a crate root; for code from an external crate, the absolute path begins with the crate name, and for code from the current crate, it starts with the literal crate.
Emod front_of_house {
    mod hosting {
        fn add_to_waitlist() {}
    }
}

pub fn eat_at_restaurant() {
    // Absolute path
    crate::front_of_house::hosting::add_to_waitlist();
}
24
Q

relative path

A
  • A relative path: starts from the current module and uses self, super, or an identifier in the current module.
Emod front_of_house {
    mod hosting {
        fn add_to_waitlist() {}
    }
}

pub fn eat_at_restaurant() {
    // Relative path
    front_of_house::hosting::add_to_waitlist();
25
Q

Starting Relative Paths with super

A

fn deliver_order() {}

mod back_of_house {
fn fix_incorrect_order() {
cook_order();
super::deliver_order();
}

fn cook_order() {} }
26
Q

Making Structs and Enums Public

A

mod back_of_house {
pub struct Breakfast {
pub toast: String,
seasonal_fruit: String,
}

impl Breakfast {
    pub fn summer(toast: &str) -> Breakfast {
        Breakfast {
            toast: String::from(toast),
            seasonal_fruit: String::from("peaches"),
        }
    }
} }

pub fn eat_at_restaurant() {
// Order a breakfast in the summer with Rye toast
let mut meal = back_of_house::Breakfast::summer(“Rye”);
// Change our mind about what bread we’d like
meal.toast = String::from(“Wheat”);
println!(“I’d like {} toast please”, meal.toast);

// The next line won't compile if we uncomment it; we're not allowed
// to see or modify the seasonal fruit that comes with the meal
// meal.seasonal_fruit = String::from("blueberries"); }
27
Q

Providing New Names with the as Keyword

A

use std::fmt::Result;
use std::io::Result as IoResult;

fn function1() -> Result {
// –snip–
}

fn function2() -> IoResult<()> {
// –snip–
}

28
Q

Using External Packages

A

To use rand (External Packages) in our project, we added this line to Cargo.toml:

Filename: Cargo.toml
~~~
rand = “0.8.5”
~~~
Adding rand as a dependency in Cargo.toml tells Cargo to download the rand package and any dependencies from crates.io and make rand available to our project.

use rand::Rng;

fn main() {
    let secret_number = rand::thread_rng().gen_range(1..=100);
}
29
Q

Using Nested Paths to Clean Up Large use Lists

A

Before:
// –snip–
use std::cmp::Ordering;
use std::io;
// –snip–

After:
// –snip–
use std::{cmp::Ordering, io};
// –snip–

Before:
// –snip–
use std::io;
use std::io::Write;
// –snip–

After:
use std::io::{self, Write};
// –snip–

30
Q

The Glob Operator

A

If we want to bring all public items defined in a path into scope, we can specify that path followed by the * glob operator:

use std::collections::*;