Andrés Alonzo
Mozilla Guatemala
@aalonzolu
curl https://sh.rustup.rs -sSf | sh
// hola.rs
fn main() {
println!("Hello, world!");
}
$ rustc hola.rs
$ ./hola
Compilar
// 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.
*/
// suma.rs
fn main() {
let a = 7;
let b = 8;
println!("La suma de {} y {} es {}", a,b, (a+b));
}
// hola.rs
fn main() {
let mut a = 7;
let mut b = 8;
}
// 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
}
// datos.rs
fn main() {
/*
Strings
*/
let cadena = "cadena";
// Booleanos
let verdadero = true;
let falso = false;
}
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);
}
}
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.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
@aalonzolu
github.com/aalonzolu
gitlab.com/aalonzolu