Packages, Crates, Modules Flashcards

1
Q

How many binary crates can a package contain?

A

Multiple.

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

How many library crates can a package contain?

A

One.

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

What is the module system in Rust?

A

A collection of features that help organize code. Features such as which code details are public or private, and what names in a program are in what scope.

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

What Rust features make up the module system?

A

Packages, Crates, Modules and Use, Paths.

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

What are packages in Cargo?

A

A Cargo feature that lets you build, test, and share crates. One or more crates that provide a set of functionality

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

What are crates in Cargo?

A

A tree of modules that produces a library or executable.

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

What do Modules do?

A

Organize code within a crate into groups for readability and easy reuse. Modules also control the privacy of items.

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

What are Paths?

A

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
9
Q

What is the Crate root file?

A

Source file that the Rust compiler starts from and makes up the root module of your crate.

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

What file does every Package contain?

A

Cargo.toml

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

What does a Cargo.toml file describe?

A

How to build the Crates in a package.

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

What must a Package contain?

A

Zero or one Library crates.

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

What is the command to create a Package?

A

cargo new

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

What convention does Cargo follow regarding src/main.rs?

A

src/main.rs is the crate root of a binary crate with the same name as the package.

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

What convention does Cargo follow regarding src/lib.rs?

A

If the package directory contains src/lib.rs then src/lib.rs is the crate root of a Library crate with the same name as the package.

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

How does cargo build a Library or a Binary?

A

By passing the crate root to rustc.

17
Q

How does a package create multiple binary crates?

A

By placing files in the src/bin directory: each file will be a separate binary crate.

18
Q

What is the syntax for defining a module?

19
Q

What do the contents of src/main.rs and src/lib.rs form?

A

A module named crate at the root of the crate’s module structure, known as the module tree.

20
Q

What does Rust use to find an item in a module tree?

21
Q

What are the two forms a path can take?

A

An absolute or a relative path.

22
Q

What is an absolute path in Rust?

A

Path that starts from a crate root by using a crate name or a literal crate.

23
Q

What is a relative path in Rust?

A

Path that starts from the current module and uses self, super, or an identifier in the current module.

24
Q

What defines Rusts privacy boundary?

25
Are all items in Rust private by default?
Yes.
26
How can you expose inner parts of child modules code to outer ancestor modules?
Using the pub keyword to make an item public.
27
What does the pub keyword on a module do?
Lets code in its ancestor module refer to it.
28
What does using super at the start of a path do?
Constructs a relative path that begins in the parent module.
29
What does pub before a struct definition do?
Make the struct public. The struct fields will still be private.
30
What does adding pub to an enum definition do?
Makes it public, along with ALL its variants.
31
How do we bring path items into scope?
With the keyword use.
32
How does using the use keyword with a relative path differ from using it with an absolute path?
The relative path must start with the keyword self.
33
What is the idiomatic way to bring a function into scope with the keyword use?
Bringing the functions parent module into scope, and not the function itself.
34
What is the idiomatic way to bring structs, enums, and other items besides functions into scope with the keyword use?
By specifying the full path.
35
What is the as keyword used for in Rust?
To provide an alias for an import or cast between types.
36
What is re-exporting?
Using pub and use to bring an item into scope but also making that item available for others to bring into their scope.