Introducción a Rust

Andrés Alonzo

Mozilla Guatemala

 

@aalonzolu

¿Quién Soy?

Desarrollador web apasionado del software libre y miembro de la comunidad Mozilla de Guatemala y C.A.

¿Qué es Rust?

Un lenguaje de programación tipado y compilado.

Instalación de Rust

curl https://sh.rustup.rs -sSf | sh

¡Hola Mundo!

// hola.rs
fn main() {
    println!("Hello, world!");
}
$ rustc hola.rs
$ ./hola

Compilar

Comentarios

    // This is an example of a line comment
    // Notice how there are two slashes at the beginning of the line
    // And that nothing written inside these will be read by the compiler

    // println!("Hello, world!");

    // Run it. See? Now try deleting the two slashes, and run it again.

    /* 
     * This is another type of comment, the block comment. In general,
     * the line comment is the recommended comment style however the
     * block comment is extremely useful for temporarily disabling
     * a large chunk of code. 
     */

Variables

// suma.rs
fn main() {
    let a = 7;
    let b = 8;
    println!("La suma de {} y {} es {}", a,b, (a+b));
}

Mutabilidad

// hola.rs
fn main() {
    let mut a = 7;
    let mut b = 8;
}

Tipos de datos

// datos.rs
fn main() {
    /* 
    Numéricos
    i8, i16, i32, i64
    u8, u16, u32, u64
    f32, f64
    */
    let x= 45; //Tipo i32
    let y= 1.0; //Tipo f64
}


Tipos de datos

// datos.rs
fn main() {
    /* 
    Strings
    */
    let cadena = "cadena";

    // Booleanos
    let verdadero = true;
    let falso = false;
}


Condiciones

fn main() {
    let n = 5;

    if n < 0 {
        print!("{} es negativo", n);
    } else if n > 0 {
        print!("{} es positivo", n);
    } else {
        print!("{} es cero", n);
    }
}

Funciones


fn main() {
    // llamar funcion
    let res = is_divisible_by(100,10);
    // imprimir resultado
    println!("Es divisible: {}", res);
}

// function que retorna un valor booleano
fn is_divisible_by(lhs: u32, rhs: u32) -> bool {
    // para evitar diviciones por 0
    if rhs == 0 {
        return false;
    }
    // rust retorna el valor de la ultima linea
    lhs % rhs == 0
}

Cargo

Cargo es el gestor de paquetes de rust

//cargo.toml
[package]
name = "hello_world" # the name of the package
version = "0.1.0"    # the current version, obeying semver
authors = ["Alice <a@example.com>", "Bob <b@example.com>"]

[dependencies]
# paquetes requeridos
paquete_1 = "1.2.0"
paquete2 = "1.1.0"
mozilla_1 = "=2.1.0"
$ cargo new Holamundo

Gracias

 

@aalonzolu

github.com/aalonzolu

gitlab.com/aalonzolu

Made with Slides.com