Last active
December 4, 2018 13:54
-
-
Save kassane/1b6f3edf5149866edcd5c5164e149a5c to your computer and use it in GitHub Desktop.
Getting Start - Rust Language Programming
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
=================================================== | |
* ESTUDANDO RUST LANGUAGE PROGRAMMING * | |
=================================================== | |
@author: Matheus Catarino | |
*/ | |
extern crate time; //request time dependency (cargo add time deps) | |
use std::io::{Write,stdin,stdout}; //write = flush | |
fn main() | |
{ | |
const YEAR_INC: i32 = 1900; | |
let d = time::now(); | |
print!("Digite um valor: "); | |
let mut pvalue = String::new(); | |
stdout().flush().expect("unknow output error!"); | |
stdin().read_line(&mut pvalue).expect("input first value error!"); | |
print!("Digite outro valor: "); | |
let mut svalue = String::new(); | |
stdout().flush().expect("unknow output error!"); | |
stdin().read_line(&mut svalue).expect("input second value error!"); | |
println!("Welcome to Rust, {} {}!", pvalue, svalue); | |
let (day, month, year) = (d.tm_mday, d.tm_mon, d.tm_year+YEAR_INC); | |
println!("Today date: {}/{}/{}", day, month, year) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(non_snake_case)] //permitir palavras sem Snake_Case | |
#![allow(dead_code)] //permitir código inútil | |
enum Direction | |
{ | |
Subir, | |
Descer, | |
Esquerda, | |
Direita | |
} | |
fn myBorrow(value: &String) -> String | |
{ | |
let mut v = value.clone(); | |
v.remove(value.len()-1); //removendo o último elemento "!" | |
v.push_str(" 2018!"); | |
v | |
} | |
struct Color { | |
red: u8, | |
blue: u16, | |
green: u32 | |
} | |
struct TpColor (u8, u16, u32); //Tuple | |
struct Rectangle | |
{ | |
width: u32, | |
height: u32 | |
} | |
impl Rectangle{ | |
fn print_description(&self) | |
{ | |
println!("\nRectangle: {} x {}", self.width, self.height); | |
} | |
} | |
struct People { | |
name: String, | |
age: u16 | |
} | |
//Traits | |
impl ToString for People { | |
fn to_string(&self) -> String{ | |
format!("\nMy name is {} and has {} years.", self.name, self.age) | |
} | |
} | |
fn main() | |
{ | |
//Structs | |
let mut class = Color {red: 255, blue: 58, green: 250}; | |
let tp_struct = TpColor(255,155,55); | |
let ret = Rectangle {width: 100, height: 50}; | |
let p1 = People {name: String::from("Lucas"), age: 18}; | |
println!("{}", p1.to_string()); | |
ret.print_description(); | |
class.blue = 236; | |
println!("\nResult:\nred:{}, blue:{}, green:{}", class.red, class.blue, class.green); //Struct | |
println!("\nResult:\nred:{}, blue:{}, green:{}", tp_struct.0, tp_struct.1, tp_struct.2); // Struct Tuple | |
let x = String::from("Hello World!"); | |
println!("\nBem Vindo ao Rust!\n '{}' é referência de X = '{}'\n", myBorrow(&x), x); | |
const CV: u32 = 34; //const | |
let x: u32 = 56; //expecificando tipo explicito ou literal ex.: (let x = Nºu32) | |
let y = if x > 270 || x < CV {0} else {x}; //suposta operação ternária/lambda (c++: '(x>valor || x < valor2) ? true : false') | |
println!("{}", y); // valor do if ternário | |
let tuple: (u32,f32,_) = (5, 2.3, ("ABC", 'd')); // expecificando tupla (int,float,anywhere) | |
let diretivo = Direction::Subir; // enum | |
println!("{}", (tuple.2).0); //print tupla dentro de tupla | |
let (b,n,m) = tuple; // distribuindo valores da tupla. Semelhante ao auto[a,b,c] (C++17) | |
let (s,w) = m; // o mesmo citado anteriormente. | |
println!("b={},n={}\ns={}, w={}", b,n,s,w); //imprimindo valores acima. | |
match diretivo { //Switch de enum | |
Direction::Subir => println!("Subir"), | |
Direction::Descer => println!("Descer"), | |
Direction::Direita => println!("Direita"), | |
Direction::Esquerda => println!("Esquerda"), | |
} | |
//3 loops types: loop(infinity), while(condition), for(n .. end) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Beginner Rust I/O | |
use std::io; | |
fn main() { | |
let mut nome = String::new(); | |
println!("Qual o seu nome?"); | |
io::stdin().read_line(&mut nome).expect("Erro inesperado!!"); | |
println!("Bem vindo ao Rust {}", nome); | |
let mut idade = String::new(); | |
println!("Qual a sua idade?"); | |
io::stdin().read_line(&mut idade).expect("Erro inesperado novamente!"); | |
println!("Sua idade é: {} anos.", idade.trim().parse::<i32>().expect("Valor inválido!")); | |
//trim -> remove spaces, parse -> conversion generic & except -> wrong msg | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(non_snake_case)] //permitir palavras sem Snake_Case | |
#![allow(dead_code)] //permitir código inútil | |
mod people; | |
use people::MCF::*; | |
fn main() | |
{ | |
let mut p = Person::new("Lucas".to_string(), 18);// {name: "Lucas".to_string(), age: 18}; | |
println!("{}", p.printinfo()); | |
p.changeName("Matheus".to_string()); | |
p.changeAge(23); | |
println!("{}", p.printinfo()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub mod MCF | |
{ | |
pub struct Person{ | |
name: String, | |
age: i16 | |
} | |
pub trait Info{ | |
fn new(name: String, age: i16) -> Person; | |
fn printinfo(&self) -> String; | |
fn changeName(&mut self, name: String); | |
fn changeAge(&mut self, age: i16); | |
} | |
impl Info for Person { | |
fn new(name: String, age: i16) -> Person | |
{ | |
Person{name: name, age: age} | |
} | |
fn printinfo(&self) -> String{ | |
format!("My Name is {} has {} years.", self.name, self.age) | |
} | |
fn changeName(&mut self, name: String){ | |
self.name = name; | |
} | |
fn changeAge(&mut self, age: i16){ | |
self.age = age; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment