Skip to content

Commit 53b2a74

Browse files
update FUNCTIONS const, add COMMENTS const
1 parent ea9f26a commit 53b2a74

1 file changed

Lines changed: 58 additions & 17 deletions

File tree

src/rustbook_code_blocks.rs

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -205,17 +205,25 @@ pub const FUNCTIONS: &str = r#"
205205
206206
Functions are used prevalently in Rust.
207207
208-
There is one of the most important funtions, the 'main' function, the entry point of many programs. In RUST we use the fn keyword to allow us to declare new functions.
209-
210-
For function and variable names, RUST uses snake case, where all letters are lowercase and underscores seperate words. example_snake_case
208+
There is one of the most important funtions, the 'main' function, the entry point of many programs.
211209
212210
// ================================================
213211
// 1. Function Basics
214212
// ================================================
213+
// For function and variable names, RUST uses snake case, where all letters are lowercase and underscores seperate words. example_snake_case
214+
// Functions are defined by the use of the fn keyword, followed by a function name and a set of parentheses, then a set of curly brackets to tell the compiler where the function body begins and ends.
215215
216-
// Functions are declared using the `fn` keyword.
217-
// The name follows snake_case convention.
218-
// Function definitions go before they are called.
216+
fn basic_function() {
217+
// This function takes no arguments and returns nothing
218+
println!("This is a basic function call.");
219+
}
220+
221+
// ================================================
222+
// 2. Defining and Calling Functions
223+
// ================================================
224+
225+
// Any function can then be called by entering its name followed by a set of parentheses.
226+
// Functions can be defined before or after your main function, as Rust does not care where you define your functions, you just need to be sure they are defined somewhere in a scope that can be seen by the caller.
219227
220228
fn main() {
221229
println!("Welcome to the Rust functions demo!");
@@ -231,18 +239,14 @@ fn main() {
231239
println!("5 + 1 = {}", result);
232240
}
233241
234-
// ================================================
235-
// 2. Defining and Calling Functions
236-
// ================================================
237-
fn basic_function() {
238-
// This function takes no arguments and returns nothing
239-
println!("This is a basic function call.");
240-
}
241-
242242
// ================================================
243243
// 3. Function Parameters
244244
// ================================================
245-
// Parameters are variables that are part of a function's signature
245+
// Functions in Rust can be defined to have parameters. Parameters in functions are special variables that are part of a functions signature. When parameters are defined for a function, you can pass it values for those parameters. Values passed into function parameters are also called arguments. But the words parameter and argument are often used interchangeably in casual conversation to describe the variables in a functions definition or the values passed in when a function is called.
246+
247+
// The type of each parameter is required to be declared in function signatures. This is deliberate by design in Rust. To determine what type you are using, The compiler almost never needs you to use them elsewhere, since the type annotations are required in function definitions.
248+
249+
// Multiple parameters are defined by separating parameter declarations with commas.
246250
247251
fn print_measurement(value: i32, unit_label: char) {
248252
// Parameters must have types explicitly declared
@@ -252,7 +256,7 @@ fn print_measurement(value: i32, unit_label: char) {
252256
// ================================================
253257
// 4. Function Return Values
254258
// ================================================
255-
// Functions can return values with the `->` syntax
259+
// Functions can return values with the `->` syntax, followed by the type of the return value.
256260
257261
fn plus_one(x: i32) -> i32 {
258262
// Implicit return: no semicolon means the value is returned
@@ -265,8 +269,18 @@ fn plus_one(x: i32) -> i32 {
265269
// ================================================
266270
// - Statements: instructions that perform actions and do not return a value
267271
// - Expressions: evaluate to a value
272+
// A series of statements optionally ending in an expression are what make up Function bodies. Rust is an expression based language and this is important to understand, as other languages don't have that same distinction.
273+
274+
// Statements are instructions that perform some action and do not return a value.
275+
Expressions evaluate to a resultant value
276+
277+
// Function definitions are statements.
268278
269-
// For example:
279+
// Statements do not have return values.
280+
281+
// For example, let y = 6 is a statement as it does not have a return value. This in not like other languages where the assignment returns the value of the assignment, such as in C or Ruby.
282+
283+
// Another example:
270284
fn _statement_vs_expression() {
271285
let y = {
272286
let x = 3;
@@ -295,5 +309,32 @@ fn multiply_add(a: i32, b: i32, c: i32) -> i32 {
295309
// - Use `->` to specify a return type
296310
// - Expression without semicolon for return
297311
// - Function calls must match parameter types
312+
"#;
313+
314+
pub const COMMENTS: &str = r#"
315+
// Demonstrates comments in Rust as described in Chapter 3.4 of The Rust Book.
298316
317+
// Comments are used to explain code, making it easier to understand.
318+
319+
fn main() {
320+
// Simple line comment
321+
// This is a single-line comment in Rust.
322+
// It starts with `//` and continues to the end of the line.
323+
324+
// Multi-line comment using repeated `//`:
325+
// Sometimes you want to write a longer explanation.
326+
// It's idiomatic in Rust to use multiple single-line comments for this.
327+
328+
/*
329+
Block comment:
330+
These are less common but can span multiple lines
331+
without needing to start each line with `//`.
332+
*/
333+
334+
let x = 5; // Inline comment: This sets x to 5
335+
336+
let y = x + 1; // Adds 1 to x and stores it in y
337+
338+
println!("x = {}, y = {}", x, y); // Prints the values
339+
}
299340
"#;

0 commit comments

Comments
 (0)