Skip to content

Instantly share code, notes, and snippets.

@marti1125
Created September 24, 2015 00:25
Show Gist options
  • Save marti1125/c76283ff6a78b3d74911 to your computer and use it in GitHub Desktop.
Save marti1125/c76283ff6a78b3d74911 to your computer and use it in GitHub Desktop.
rust
struct Point {
x: i32,
y: i32,
}
fn main() {
let mut point = Point { x: 0, y: 0 };
point.x = 5;
println!("The point is at ({}, {})", point.x, point.y);
}
/*
// WHILE
let mut x = 5; // mut x: i32
let mut done = false; // mut done: bool
while !done {
x += x - 3;
println!("{}", x);
if x % 5 == 0 {
done = true;
}
}
// FOR
for x in 0..10 {
println!("{}", x); // x: i32
}
*/
/*
// LOOP INFINITO
loop {
println!("Loop forever!");
}
*/
/*
let x = 5;
if x == 5 {
println!("x is five!");
} else if x == 6 {
println!("x is six!");
} else {
println!("x is not five or six :(");
}
let x = 5;
let y = if x == 5 {
10
} else {
15
}; // y: i32
println!("the value of y is :{:?}",y);
*/
/*
// SLICES
let a = [0, 1, 2, 3, 4];
let middle = &a[2..4]; // A slice of a: just the elements 1, 2, and 3
let complete = &a[..]; // A slice containing all of the elements in a
println!("middle: {:?}", middle);
println!("complete: {:?}", complete);
// TUPLES
let x = (1, "hello"); //let x: (i32, &str) = (1, "hello");
let (x, y, z) = (1, 2, 3);
println!("x is {}", x);
// TUPLE INDEXING
let tuple = (1, 2, 3);
let a = tuple.0;
let b = tuple.1;
let c = tuple.2;
println!("a is {}", a);
*/
/*call("Willy".to_string());
println!("ADD: {:?}", add(2,9));
let x = '♞'; // char
// Booleans
let t = true;
let f: bool = false;
// Tipos Numericos
let n1 = 42; // x has type i32
let float = 1.0; // y has type f64
let names = ["Graydon", "Brian", "Niko"]; // names: [&str; 3]
println!("a has {} elements",names.len());
println!("The second name is: {}", names[1]);
*/
fn add(n1 :i32, n2 : i32) -> i32 {
n1 + n2
}
fn call(name :String) {
println!("Hello, {:?}", name)
}
#[test]
#[should_panic]
fn it_works() {
assert!(false);
}
#[test]
#[should_panic(expected = "assertion failed")]
fn eq() {
assert_eq!("Hello", "world");
}
pub fn add_two(a: i32) -> i32 {
a + 2
}
#[test]
fn check_add() {
assert_eq!(4, add_two(2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment