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