Coder Social home page Coder Social logo

learning_rust's Introduction

Learning Rust

Little programs to learn the basic concepts of the Rust programming language

Useful sites

Specific information

Compilation

With cargo init we create a proyect in which we can use cargo run to compile and run our program. The binaty is saved in target/debug/<file_name>.

To compile just one file we can use:

rustc <nombre_del_fichero>.rs

Variables

Variables are declared using the let keyword Types are infered by the compiler but they can be added to the variable declaration Variables are inmutable by default. mut keyword can be used to make a variable mutable

fn main () {
	let x = 69;
	let y: f64 = 2.71;
	let mut z: i64 = 434243234545;
}

In Rust, variable names are written in snake_case

Types

  • bool -> true, false
  • unsigned integer -> u8, u16, u32, u64, u128
  • signed integer -> i8, i16, i32, i64, i128
  • floating -> f32, f64

Arrays

Fixed lenght collection of elements all of the same type. C indexation style.

fn main() {
    let nums: [i32; 3] = [1, 2, 3];
    println!("{:?}", nums);
    println!("{}", nums[1]);
}
[1, 2, 3]
2

To make dynamic arrays use Vectors

Functions

Always in snake_case. If you just want to return an expression, you can drop the return keyword and the semicolon at the end, as we did in the subtract function.

fn add(x: i32, y: i32) -> i32 {
    return x + y;
}

fn subtract(x: i32, y: i32) -> i32 {
    x - y
}

fn main() {
    println!("42 + 13 = {}", add(42, 13));
    println!("42 - 13 = {}", subtract(42, 13));
}

If no return type is specified for a function, it returns an empty tuple, also known as a unit like in ocaml. An empty tuple is represented by ().

Tuples

Tuple elements can be referenced by their index number. (like c structs)

fn swap(x: i32, y: i32) -> (i32, i32) {
    return (y, x);
}

fn main() {
    // return a tuple of return values
    let result = swap(123, 321);
    println!("{} {}", result.0, result.1);

    // destructure the tuple into two variables names
    let (a, b) = swap(result.0, result.1);
    println!("{} {}", a, b);
}

learning_rust's People

Contributors

daniqss avatar

Stargazers

Luca D'angelo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.