Introduction to Rust - Part 2: Functions and Modules

Опубликовано: 25 Июнь 2026
на канале: Rhymu's Videos
2,909
143

This is the 2nd in a series of videos that cover, step by step, learning to code using the Rust programming language.

In this video we'll cover functions and modules, how to bring in external dependencies, and setting up a few more VSCode plugins to help when working with Rust.

0:00 - Introduction
0:37 - What is a function?
0:45 - Studying our "main" function
1:12 - "main": special name for program's top-level function
1:35 - Adding another function
2:35 - Calling functions
3:16 - Aside: installing rust-analyzer plugin
4:53 - Why divide code up into separate functions?
5:25 - Using modules to organize groups of functions
6:24 - Explanation: how we moved a function into a module
7:30 - Aside: set up auto-formatting
8:48 - Bringing in dependencies; external crates
10:11 - Aside: installing crates plugin
10:53 - How to specify external dependencies and versions
11:40 - Nesting and splitting modules up between files
11:58 - Nesting one module inside another module
12:34 - Splitting modules up between files
13:26 - Importing/aliasing names from other modules
14:12 - Renaming things using the "as" keyword
15:23 - In Summary

How to set up auto-formatting of Rust in VSCode:

Install a plugin supporting formatting Rust, such as rust-analyzer.
Add to your VSCode settings (settings.json) the following:

"[rust]": {
"editor.formatOnSave": true
}

*Optional:*
If you make a file "rustfmt.toml" and put it in the top folder of your project, rustfmt will look there for custom settings. Personally, I prefer some settings that are only available in the "nightly" build. To make this work, Idid these extra steps:

Install "nightly" build of rust, with this command:
`rust toolchain install nightly`
Tell rust-analyzer to use the "nightly" build when it runs `rustfmt`, by adding this section to your VSCode settings (settings.json):

"rust-analyzer.rustfmt.extraArgs": {
"+nightly"
}

Here's what my "rustfmt.toml" file looks like if you're curious:

A lot of the styles we want can only be configured
using unstable features.
# If you have trouble running rustfmt, try "cargo +nightly fmt".
unstable_features = true

edition = "2021"
fn_args_layout = "Vertical"
fn_call_width = 80
format_code_in_doc_comments = true
imports_granularity = "Crate"
imports_layout = "Vertical"
match_block_trailing_comma = true
max_width = 80
newline_style = "Native"
normalize_comments = true
normalize_doc_attributes = true
overflow_delimited_expr = true
reorder_impl_items = true
struct_lit_single_line = false
use_field_init_shorthand = true
use_small_heuristics = "Off"
use_try_shorthand = true
wrap_comments = true

These are already set to the defaults, but they are things
we really want to make sure are set, just in case defaults change.
reorder_imports = true
reorder_modules = true
space_after_colon = true
space_before_colon = false
tab_spaces = 4