Rust Basics Flashcards
What is the naming convention for functions and methods in Rust?
snake_case
What is the naming convention for local variables in Rust?
snake_case
What is the naming convention for types and traits in rust?
UpperCamelCase
How would you make a new directory for a projects folder with a hello_world folder in that directory with Powershell?
- Ensure you are in wsl (type ‘wsl’)
- Type the following:
~~~
$ mkdir ~/projects
$ cd ~/projects
$ mkdir hello_world
$ cd hello_world
~~~
What is proper file naming convention in rust?
- .rs rust extension
- More than one word in filename uses underscores to separate with all lowercase
Ex: hello_world.rs
How do you write a function in Rust with “Hello, world!” as the output?
fn main() { println!("Hello, world!"); }
What is always the first piece of code that runs in every executable Rust program?
fn main() { }
What is Cargo?
- 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 do you check the version of Cargo in rust?
cargo –version
How would you create a new project using cargo?
$ 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
What is the Cargo.toml file in Rust?
- 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 do you build and/or run Rust programs with cargo?
- Make sure you are in the correct directory (use cd <file_name> to navigate to the correct file path
- Type ‘cargo run <program_name>' or simply 'cargo build' if it is just one program file</program_name>
What kind of variables are made by default in Rust?
- 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 do we make a variable immutable in Rust?
- Use the ‘mut’ keyword:
let mut bananas = 5; // mutable
How do you define a string in Rust?
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
What is the standard library for input/output in Rust and how do you include it in the program?
use std::io;
What does the following code do?
io::stdin() .read_line(&mut guess)
- 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)
How do you handle situations where the read_line operation fails?
- Use ‘.expect’
- Ex:
io::stdin() .read_line(&mut guess) .expect("Failed to read line");
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?
$ 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!
Break down what each line of this simple program does:
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: **