Rust Basics Flashcards

1
Q

What is the naming convention for functions and methods in Rust?

A

snake_case

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

What is the naming convention for local variables in Rust?

A

snake_case

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

What is the naming convention for types and traits in rust?

A

UpperCamelCase

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

How would you make a new directory for a projects folder with a hello_world folder in that directory with Powershell?

A
  1. Ensure you are in wsl (type ‘wsl’)
  2. Type the following:
    ~~~
    $ mkdir ~/projects
    $ cd ~/projects
    $ mkdir hello_world
    $ cd hello_world
    ~~~
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is proper file naming convention in rust?

A
  • .rs rust extension
  • More than one word in filename uses underscores to separate with all lowercase

Ex: hello_world.rs

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

How do you write a function in Rust with “Hello, world!” as the output?

A
fn main() {
    println!("Hello, world!");
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is always the first piece of code that runs in every executable Rust program?

A
fn main() {

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

What is Cargo?

A
  • Rust’s build system and package manager
  • Handles tasks such as building you code, downloading the libraries the code depends on, and building those libraries

(another name for libraries is dependencies)

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

How do you check the version of Cargo in rust?

A

cargo –version

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

How would you create a new project using cargo?

A
$ cargo new hello_cargo
$ cd hello_cargo
  • First command creates a new directory and project called hello_cargo
  • Cargo automatically generates two filed under this directory: Cargo.toml and an src directory with a main.rs file
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the Cargo.toml file in Rust?

A
  • TOML (Tom’s Obvious, Minimal Language) format, is Cargo’s configuration format
  • Displays the configuration package and the config information Cargo needs to complile the program
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you build and/or run Rust programs with cargo?

A
  1. Make sure you are in the correct directory (use cd <file_name> to navigate to the correct file path
  2. Type ‘cargo run <program_name>' or simply 'cargo build' if it is just one program file</program_name>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What kind of variables are made by default in Rust?

A
  • Variables are immutable by default, meaning once we give it a value, that value won’t change
  • Ex:
    ~~~
    let apples = 5; // immutable
    ~~~
  • Must de
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do we make a variable immutable in Rust?

A
  • Use the ‘mut’ keyword:
    ~~~
    let mut bananas = 5; // mutable
    ~~~
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you define a string in Rust?

A

String::new() is a function that returns a new instance of a string
* Ex:
~~~
let mut guess = String::new();
~~~
* The above code created a mutable variable that is currently bound to a new empty instance of a string

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

What is the standard library for input/output in Rust and how do you include it in the program?

A
use std::io;
17
Q

What does the following code do?
~~~
io::stdin()
.read_line(&mut guess)
~~~

A
  • io::stdin() Takes in user input
  • readline takes whatever the user types into stdin and appends it to the string passed into the argument. (string that is passed in must be mutable)
18
Q

How do you handle situations where the read_line operation fails?

A
  • Use ‘.expect’
  • Ex:
    ~~~
    io::stdin()
    .read_line(&mut guess)
    .expect(“Failed to read line”);

~~~

19
Q

How do you check to see if your program compiles but you do not want to create an executable to run the program?
What is the advantage to using this command over cargo build?

A
$ cargo check
  • cargo check is much faster than cargo build because it skips the step of producing an executable. If you’re continually checking your work while writing the code, using cargo check will speed up the process of letting you know if your project is still compiling!
20
Q

Break down what each line of this simple program does:

A

Lets break it down by line:
**Line 1: **Input/output library in Rust that allows us to obtain user input and print the output. io comes from rusts standard library, std
**Lines 4 & 6: ** Simple print statements
**Line 8: ** Creates a mutable variable guess and calls the function String
**Line 1: **
**Line 1: **
**Line 1: **