Commit 9bedcf32 authored by Mathieu Nivoliez's avatar Mathieu Nivoliez

Add post in draft mode.

parent 7c61383d
extends: post.liquid
comments: true
title: 'Getting started with Rust: Function, struct and ref'
date: 05 Dec 2017 00:00:00 +0000
author: Mathieu Nivoliez
draft: true
lang: en
Hello everyone! Today we continue on our rust serie, we are going to talk about function and struct.
## "Seems like a lot? Are we really going to cover all this?"
Yes and yes.
In order to do all that, we are going to need a variable, let's define that:
// We want to buy a fallout boy, so lets define a number of caps.
let caps = 5;
We just define a variable caps of value 5.
## "Wait a minute! In the previous post, didn't you said that rust was a strongly typed language?"
I did, and it's true. Let me explain you the beautiful world of type inference!
At compile time, the rust compiler will try to guess the type of `caps` from its use (assignation or operation).
I said *try* because, sometimes, the rust compiler will not succeded. For example, if the using of caps not allow to define its type.
The type inference is here to avoid writting types when it can easily be guessed.
You still can define type with this syntax:
let caps: usize /* type define here */ = 5;
That said, a variable can be shadowed. Imagine that you got the number of caps in the form of a string, it makes senses to still call it caps. That's what shadowing is about.
Let me show you:
let caps = "five";
let caps = get_number_from_string(caps); // caps is now 5
It may not be intuitive yet, but through practice, it will.
## "Ok, but what about this *get_number_from_string* thing?"
Excellent transition, it's almost like if I was writting your line.
*get_number_from_string* is a function. Let see how we can define it:
fn get_number_from_string( num: &'static str ) -> usize {...}
## "HOLD ON!!!! WHAT IS THIS?????"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment