From the course: First Look: Rust
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
How functions work
- [Narrator] A function is a block of organized, reusable code that is used to perform a single related action. Functions provide better modularity for your application and a high degree of code reusing. Let me just open up "hello_word.rs," the first Rust file we've written. We've already seen one of the most important functions in the language, the main function, which is the entry point of many programs. We have also seen the fn keyword, which allows you to declare new functions. Rust code uses snake case as the conventional style for function and variable names. In snake case, all letters are lowercase and underscores separate words. Function definitions in Rust start with "fn" and have a set of parentheses after the function name. The curly brackets tell the compiler where the function body begins and where it ends. Let's open up our command prompt and start a new binary project named "functions." You know how to do it. "Cargo new functions" followed by "bin." This creates the…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
Variables and mutability3m 15s
-
(Locked)
Scalar data types: Integers4m 2s
-
(Locked)
Scalar data types: Floats2m 10s
-
(Locked)
Compound data types: Tuples3m 50s
-
(Locked)
Compound data types: Arrays3m 55s
-
(Locked)
How functions work4m 12s
-
(Locked)
Return a value from a function3m 44s
-
(Locked)
Control flow: if/else6m 9s
-
(Locked)
Control flow: While loops3m 19s
-
(Locked)
Control flow: For loops2m 9s
-
-
-
-
-
-