Getting Started Flashcards

1
Q

Does Rust require a Linker installed on your system?

A

Yes. C compilers usually come with the correct linker. Also, some common Rust packages depend on C code and will need a C compiler

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

What is the command to update rust to the latest stable version?

A

rustup update stable

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

Rustup installs local documentation. What is the command to open it?

A

rustup doc

This will open the documentation in your browser.

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

What is the extension for Rust files?

A

*.rs

By convention, Rust filenames use snake case with underscores between words.

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

What function runs first when a Rust program is executed?

A

main(), though there is a small Rust “runtime” which runs before this, then calls main() as the first function to execute. The body of main() must be surrounded by curly braces.

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

What is the tool to format Rust code in the established style?

A

rustfmt

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

Does Rust indent code with tabs or spaces?

A

The Rust convention is to indent with 4 spaces.

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

What is println!() an example of?

A

A macro.

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

What character ends an expression?

A

The semicolon (;) character indicates that an expression is over and the next one is ready to begin.

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

How do you compile a Rust file using the Rust compiler?

A

Enter: rustc file_name.rs

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

What is the output of the Rust compiler?

A

A binary executable.

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

Rust is an ahead-of-time compiled language. What does this mean?

A

You can compile an executable and give it to someone else, and they can run it without having Rust installed.

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

What is Rust’s build system and package manager called?

A

Cargo

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

What is the name for libraries your code needs?

A

Dependencies

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

What does the command “cargo new hello_world” do?

A

It initialises a new Rust executable package. It will create a directory called “hello_world”. In this directory it will create a Cargo.toml file and a src directory containing the file main.rs. It will also create a .gitignore file unless the –vcs switch is used to use another VCS or none at all.

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

What does TOML stand for?

A

Tom’s Obvious, Minimal Language. It is Cargo’s configuration format.

17
Q

What is the [package] section of Cargo.toml?

A

[package] is a section heading indicating that the following statements are configuring a package.

18
Q

What are the 4 minimum pieces of information in Cargo.toml needed for Cargo to compile a package?

A

name, version, authors, edition

19
Q

Where does Cargo get your name and email information from?

A

Your environment

20
Q

What is the [dependencies] section of the Cargo.toml file?

A

It is the start of the section to list any of your project’s dependencies. In Rust, packages of code are referred to as crates.

21
Q

Where does Cargo expect to find an executable’s source files?

A

In the src directory.

22
Q

What type of files should go in a project’s top-level directory?

A

README files, license information, configuration files and anything else not related to your code.

23
Q

If you started a project that doesn’t use Cargo, how do you convert it to a project which does?

A

Move the project source code to a src directory, and create a Cargo.toml file in the top level directory.

24
Q

Running the command ‘cargo build’ causes Cargo to create the Cargo.lock file. What does this file do?

A

Cargo.lock is created in the top-level directory and keeps track of the exact version of project dependencies. You won’t ever need to change this file manually.

25
Q

What does running the command ‘cargo check’ do?

A

It checks your code to ensure it compiles, but doesn’t create an executable. It is often much faster than ‘cargo build’ because it doesn’t create the executable.

26
Q

What does the command: ‘cargo build –release’ do?

A

It build the executable with optimisations in the target/release folder. Optimisations make Rust code run faster but increase build time. It should always be used when benchmarking code’s running time.

27
Q

What are the 2 standard build profiles?

A

development and release

28
Q

How do you make the Cargo output less verbose?

A

Use the -q | –quiet option

29
Q

What is the Rust unit type?

A

The unit type is like an empty value and is signified with a set of empty parentheses: (). It is used when there is no other meaningful value that could be returned. A function without a return type eg main() returns the unit type.

30
Q

Who created Rust?

A

First appearing in 2010 out of a personal project started in 2006, Rust was designed by Graydon Hoare at Mozilla Research with contributions from Dave Herman, Brendan Eich, and others.

31
Q

In Rust, is if an expression or a statement?

A

In Rust, if is an expression, not a statement. An expression returns a value, but a statement does not. An if without an else will return the unit type.

32
Q

What does the ? operator do?

A

Where a function returns a Result, the ? operator will unpack an Ok value or propagate the Err value to the return type.

33
Q

What is the idiomatic way to return a value from a function?

A

Whilst Rust does have the return keyword to return a value from a function, the idiom is to omit the semicolon from the last expression to implicitly return that result.

34
Q

How do you separate flags meant for cargo from flags meant for the binary it will run?

A

Use a pair of dashes:

Cargo run -q — -n

35
Q

Where does cargo place downloaded source code?

A

Into the .cargo/registry/sec/ folder in your home directory.

36
Q

Where does cargo build the dependencies?

A

In the project’s target/debug/deps directory.

37
Q

How do you delete the target directory?

A

cargo clean will delete the target directories, including all the built dependencies. This can free up disk space for projects no being worked on.

38
Q

What is the purpose of a leading underscore in a variable name?

A

It suppresses compiler warnings that the variable is unused, which is useful during development.

39
Q

What are the Rust release channels

A

Stable, Beta and Nightly