Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying The Rust Programming Handbook
  • Table Of Contents Toc
The Rust Programming Handbook

The Rust Programming Handbook

By : Francesco Ciulla
close
close
The Rust Programming Handbook

The Rust Programming Handbook

By: Francesco Ciulla

Overview of this book

The Rust Programming Handbook is a deeply engaging and meticulously crafted book designed to immerse programmers into the intricate world of Rust’s core principles and sophisticated features. This book not only enhances your coding skills but also prepares you to tackle complex challenges in software development, optimizing your code for better performance and reliability. You will explore Rust’s powerful concurrency models, rigorous memory safety guarantees, and its versatile trait system. Discover the foundational elements that make Rust a standout language for developing safe and efficient applications. The book will show you how these core principles can seamlessly transition into real-world applications. You will learn how to apply Rust's capabilities to systems programming and web development, extending the reach of its safety and efficiency benefits across different programming domains. Whether it's creating low-level system components or high-performance web services, the book provides practical examples to integrate Rust effectively into a variety of projects. Elevate your coding skills and become a sought-after professional in the tech industry with this essential guide. Rust from Beginner to Professional is your definitive toolkit for mastering advanced Rust programming techniques and writing high-quality code.
Table of Contents (20 chapters)
close
close
18
Other Books You May Enjoy
19
Index

Functions

In this final section of the chapter, we’ll explore functions, a fundamental building block of Rust programs. Functions allow you to encapsulate and reuse logic throughout your code, making it more modular and easier to maintain. Defining and using functions is essential for writing effective Rust programs.

Functions in Rust are defined using the fn keyword, followed by the function name, parameters, and the function body. Let’s define a simple function to see how it works.

Functions example

In Rust, functions can be defined globally (outside main) or locally (inside another function). This example shows both approaches:

// Global function (available everywhere)
fn add(a: i32, b: i32) -> i32 {
    a + b
}
fn main() {
    // Local function (only available inside main)
    fn greet(name: &str) {
        println!("Hello, {}!", name);
    }
    // Call the local function
    greet("Alice");
    greet("Bob");
    // Call the global function
    let sum = add(1, 2);
    println!("The sum is: {}", sum);
}

In this example, we see both the function definition and the function invocation:

  • Function definition:
    • The greet function takes a single parameter name of the &str type (a string slice), and prints a greeting message. This demonstrates how to define a function and use parameters.
    • The add function takes two parameters (a and b) of the i32 type (32-bit integers) and returns their sum. This demonstrates how to define a function that returns a value.
  • Function invocation:
    • We call the greet function twice with different arguments ("Alice" and "Bob"), demonstrating how to pass arguments to functions.
    • We call the add function with arguments of 5 and 7, store the result in the variable sum, and print it.

These are just a few of the core syntax concepts in Rust. In this chapter, we’ve introduced the basics of Rust programming, including variables and mutability, data types, control flow, and functions. Each of these topics is crucial for building a solid foundation in Rust.

As we progress through the book, we’ll explore more advanced topics and dive deeper into Rust’s syntax and features. Each chapter will provide detailed explanations, practical examples, questions, and assignments to help you master Rust programming. By the end of this book, you’ll have a comprehensive understanding of Rust and be well equipped to tackle complex programming challenges confidently.

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
The Rust Programming Handbook
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon