Skip to content

Instantly share code, notes, and snippets.

@thorhop
Created April 20, 2020 00:34
Show Gist options
  • Save thorhop/f6073b1c92ba15a4b969f9f886cfdae8 to your computer and use it in GitHub Desktop.
Save thorhop/f6073b1c92ba15a4b969f9f886cfdae8 to your computer and use it in GitHub Desktop.
"The Book" (on Rust): Chapter 3.2 "Data Types".
// manual title: Data Types
// manual page: https://doc.rust-lang.org/book/ch03-02-data-types.html
fn numfail() {
// put anything else but u32 number in the variable
// and it will fail
let guess: u32 = "42".parse().expect("Needs to be a u32 number");
println!("Message: {}", guess);
}
fn fpnums() {
let x = 2.0; // f64
let y: f32 = 3.0; // f32
println!("{}", x);
println!("{}", y);
}
fn numops() {
// addition
let sum = 5 + 10;
println!("{}", sum);
// subtraction
let difference = 95.5 - 4.3;
println!("{}", difference);
// multiplication
let product = 4 * 30;
println!("{}", product);
// division
let quotient = 56.7 / 32.2;
println!("{}", quotient);
// remainder
let remainder = 43 % 5;
println!("{}", remainder);
}
fn chartype() {
let _c = 'z';
println!("{}", _c);
let _z = 'ℤ';
println!("{}", _z);
let _heart_eyed_cat = '😻';
println!("{}", _heart_eyed_cat);
}
fn tuptype() {
// A tuple is a general way of grouping together a number of values with a variety of types into one compound type.
// Tuples have a fixed length: once declared, they cannot grow or shrink in size.
let tup: (i32, f64, u8) = (500, 6.4, 1);
// variables underlined because the variables are "unused" according to compiler
let (_x, _y, _z) = tup;
println!("The value of _y is: {}", _y);
println!("The value of _x is: {}", _x);
println!("The value of _z is: {}", _z);
let x: (i32, f64, u8) = (500, 6.4, 1);
//let five_hundred = x.0;
//let six_point_four = x.1;
//let one = x.2;
println!("the first value of tup 'x' is {}", x.0);
println!("the second value of tup 'x' is {}", x.1);
println!("the third value of tup 'x' is {}", x.2);
}
fn arrays() {
// Another way to have a collection of multiple values is with an array.
// Unlike a tuple, every element of an array must have the same type.
// Arrays in Rust are different from arrays in some other languages
// because arrays in Rust have a fixed length, like tuples.
let a = [1, 2, 3, 4, 5];
let first = a[0];
let second = a[1];
println!("the two first values of the a array are {} and {}", first, second);
let monthsarray = ["January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"];
println!("The month of the year are: \n {:#?}", monthsarray);
let october = monthsarray[9];
println!("The heavy metal month is: {}", october);
let b: [i32; 5] = [1, 2, 3, 4, 5];
let thirdarr = b[2];
println!("The third number in the i32 array is: {}", thirdarr);
let c = [3; 5];
println!("The objects in the array are:\n {:#?}", c);
}
fn arraybroke() {
let a = [1, 2, 3, 4, 5];
let index = 10;
let element = a[index];
println!("The value of element is: {}", element);
}
fn main() {
numfail();
fpnums();
numops();
chartype();
tuptype();
arrays();
arraybroke(); // will cause a PANIC
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment