Logo

dev-resources.site

for different kinds of informations.

Day 7: Unleashing Functions in Rust πŸš€

Published at
1/7/2024
Categories
rust
100daysofcode
programming
self
Author
aniket_botre
Author
12 person written this
aniket_botre
open
Day 7: Unleashing Functions in Rust πŸš€

Greetings, code enthusiasts! On Day 7 of my #100DaysOfCode journey with Rust, let's dive into the world of functions - the building blocks of any great code composition.

Declaring Functions: The Rustic Way πŸ› οΈ

In Rust, functions start with the fn keyword, followed by a name, parentheses, and curly braces. You've already met the star of the show, the main function - the entry point of every Rust program:

fn main() {
    greet();
}

fn greet() {
    println!("Greetings, Rustacean!");
}
Enter fullscreen mode Exit fullscreen mode

Quick Notes on Rustic Etiquette πŸ“œ

  • Function names in Rust embrace snake_case: all lowercase with underscores between words.

  • Choose names that are both descriptive and expressive to convey the function's purpose clearly.

  • Avoid starting function names with numbers for the sake of readability.


Adding Flavor: Functions with Parameters 🌢️

Let's spice things up with functions that take parameters:

fn main() {
    greet("John Doe");
}

fn greet(name: &str) {
    println!("Greetings, {name}!!");
}

Enter fullscreen mode Exit fullscreen mode

Note: Explicitly mention the parameter type for a dash of clarity.


Return of the Function: Functions with Return Values πŸ”„

Witness the grand return of functions with values:

fn main() {
    let result = add(10, 20);
    println!("The sum is {}", result);
}

fn add(a: i32, b: i32) -> i32 {
    a + b
}
Enter fullscreen mode Exit fullscreen mode

Note: Indicate the return type after an arrow -> for a spectacular finale.

As the symphony of functions continues to play in Rust, I find joy in crafting code with clarity and purpose. Join me on Github for more updates! πŸ’»πŸŒβœ¨

RustLang #Programming #LearningToCode #CodeNewbie #TechJourney #Day7

self Article's
30 articles in total
Favicon
Self hosted supabase setup with Authelia and Caddy
Favicon
Microsoft Certification Courses
Favicon
Hello World!
Favicon
Embracing Self-Care: Your Path to Wellness
Favicon
self, Self, and Self.self in Swift
Favicon
Day 14: Rust's Symphony of Generics and Traits – Crafting Code for the Real World πŸŒπŸš€
Favicon
Day 13: Rust Enums – Unleashing the Power of Variants! πŸ’ͺπŸ¦€
Favicon
Day 12: Navigating the Rustic Terrain of Struct Methods 🏞️
Favicon
Day 10: Rustic Riddles - Unleashing the Number Guessing Game 🎲
Favicon
Day 7: Unleashing Functions in Rust πŸš€
Favicon
Day 5: Mastering Rust's Conditionals and Match Expressions
Favicon
Mindful Practices that help me get out of my own head and my way
Favicon
What Weightlifting Has Taught Me About Systems Engineering
Favicon
The Mindset Shift that Transform My Long-Term Outlook
Favicon
Embracing the Power of Being Second
Favicon
The 9-to-5 Scholar: Completing MIT OCW CSE with a Full time Job
Favicon
2022 retro, and what's up
Favicon
Find Prime Number in C++
Favicon
Top 6 Sites para Encontrar, Gerar, Criar e Compartilhar Memes
Favicon
Day-2 of Machine Learning
Favicon
The worst piece of life advice I ever received
Favicon
Como saber e validar o dΓ­gito verificador do RG (Registro Geral)
Favicon
Como descobrir qual a espΓ©cie de planta por foto?
Favicon
Orientação a objetos baseada em protótipos parte 2
Favicon
Top ways to become a self taught developer
Favicon
Why Polywork?
Favicon
Become a better front-end developer
Favicon
"Sophie's World in Ruby"
Favicon
Melhores Sites para Encontrar EstΓ‘gios do Brasil
Favicon
Melhores Sites de Emprego do Brasil

Featured ones: