main.rs fn main() { println!("Hello, QConSP!"); println!( “My name is Elemar Júnior!" ); println!( “falecom@elemarjr.co" ); println!( “@elemarjr" ); }
main.rs fn main() { println!("Hello, World!"); }
main.rs fn main() { println!("Hello, World!"); } $ rustc main.rs
$ cargo new hello_cargo
$ cargo run
$ cargo check
$ cargo build --release
main.rs use std::io; fn main() { println!("Please, enter a value: "); let mut value = String::new(); match io::stdin().read_line(&mut value) { Ok(n) => { println!("{} bytes read", n); println!("{}", value); } Err(error) => println!("error: {}", error) } }
Cargo.toml [package] name = "hello_cargo" version = "0.1.0" authors = ["Elemar Jr <elemarjr@gmail.com>"] edition = "2018" [dependencies] rand = "0.6.5"
main.rs use std::io; use std::cmp::Ordering; use rand::Rng; fn main() { println!("Guess the number!"); let secret_number = rand::thread_rng().gen_range(1, 101); loop { println!("Please input your guess."); let mut guess = String::new(); io::stdin().read_line(&mut guess) .expect("Failed to read line"); let guess: u32 = match guess.trim().parse() { Ok(num) => num, Err(_) => continue, }; println!("You guessed: {}", guess); match guess.cmp(&secret_number) { Ordering::Less => println!("Too small!"), Ordering::Greater => println!("Too big!"), Ordering::Equal => { println!("You win!"); break; } } } }
main.rs fn main() { println!("Number is {}", number()); } fn number() -> i32 { 5 }
main.rs fn main() { println!("Number is {}", number(5)); } // fn number() -> i32 { // 5 // } fn number(n: i32) -> i32 { n + 1 }
main.rs fn main() { let x = 5; let y = { let x = 2; x + 2 }; println!("X is {} and Y is {}", x, y); }
main.rs fn main() { let a = 5; let b = a; println!("A is {} and B is {}", a, b); }
main.rs fn main() { let a = String::from("Foo"); let b = a; println!("A is {} and B is {}", a, b); }
main.rs fn main() { let a = String::from("Foo"); let b = a; println!("A is {} and B is {}", a, b); }
main.rs fn main() { let a = String::from("Foo"); let b = a.clone(); println!("A is {} and B is {}", a, b); }
main.rs fn main() { let a = String::from("Foo"); print_string(a); println!("A is {}", a); } fn print_string(s: String) { println!("{}", s); }
main.rs fn main() { let a = String::from("Foo"); print_string(a); println!("A is {}", a); } fn print_string(s: String) { println!("{}", s); }
main.rs fn main() { let a = String::from("Foo"); let a = print_string(a); println!("A is {}", a); } fn print_string(s: String) -> String { println!("{}", s); s }
main.rs fn main() { let a = String::from("Foo"); let (_, length) = get_string_length(a); println!("Length of A is {}.", length); } fn get_string_length(s: String) -> (String, usize) { let length = s.len(); (s, length) }
main.rs fn main() { let a = String::from("Foo"); let (_, length) = get_string_length(a); println!("Length of A is {}.", length); } fn get_string_length(s: String) -> (String, usize) { (s, s.len()) }
main.rs fn main() { let a = String::from("Foo"); let length = get_string_length(&a); println!("Length of A is {}.", length); } fn get_string_length(s: &String) -> usize { s.len() }
main.rs fn longest(x: &str, y: &str) -> &str { if x.len() > y.len() { x } else { y } }
main.rs fn longest(x: &str, y: &str) -> &str { if x.len() > y.len() { x } else { y } }
main.rs fn main() { let r; { let x = 5; r = &x; } println!("r: {}", r); }
main.rs fn main() { let r; let s1 = String::from("Elemar"); { let s2 = String::from("Jr"); r = longest(&s1, &s2); } println!("r: {}", r); } fn longest(x: &str, y: &str) -> &str { if x.len() > y.len() { x } else { y } }”
main.rs fn main() { let r; let s1 = String::from("Elemar"); { let s2 = String::from("Junior S"); r = longest(&s1, &s2); } println!("r: {}", r); } fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { if x.len() > y.len() { x } else { y } }
main.rs fn main() { let r; let s1 = String::from("Elemar"); let s2 = String::from("Junior S"); r = longest(&s1, &s2); println!("r: {}", r); } fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { if x.len() > y.len() { x } else { y } }
main.rs use std::io; fn main() { let mut s1 = String::new(); let mut s2 = String::new(); io::stdin().read_line(&mut s1) .expect("Failed to read the first string"); io::stdin().read_line(&mut s2) .expect("Failed to read the second string"); match longest(&s1, &s2) { Some(x) => println!("{}", x), None => println!("Same size"), }; } fn longest<'a>(x: &'a str, y: &'a str) -> Option<&'a str> { if x.len() > y.len() { Some(x) } else if x.len() < y.len() { Some(y) } else { None } }
main.rs fn main() { let full_name = ("Elemar", "Junior“, 39 ); let (_, last_name, age) = full_name; println!("{}", last_name); }
main.rs fn main() { let full_name = FullName { first_name: String::from("Elemar"), last_name: String::from("Jr"), age: 39, }; println!("{}", full_name.last_name); } struct FullName { first_name: String, last_name: String, age: u8 }
main.rs fn main() { let p = Point3 { x: 30.0, y: 40.0, z: 0.0}; print!("{}", p.length()); } struct Point3 { x: f32, y: f32, z: f32 } impl Point3 { fn length(&self) -> f32 { ( self.x * self.x + self.y * self.y + self.z * self.z ).sqrt() } fn unit(&self) -> Point3 { let l = self.length(); Point3 { x: &self.x / l, y: &self.y / l, z: &self.z / l, } } }
main.rs impl std::fmt::Display for Point3 { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "({}, {}, {})", self.x, self.y, self.z) } }
main.rs fn main() { let p = Point3 { x: 30.0, y: 40.0, z: 0.0}; print!("{}", p); } impl std::fmt::Display for Point3 { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "({}, {}, {})", self.x, self.y, self.z) } }
main.rs geometry.rs mod geometry; pub struct Point3 { pub x: f32, use geometry::Point3 as Point; pub y: f32, use std::fmt::*; pub z: f32 } fn main() { let p = Point { impl Point3 { x: 30.0, pub fn length(&self) -> f32 { y: 40.0, ( z: 0.0, self.x * self.x + }; self.y * self.y + self.z * self.z println!("P is {} length = {}", p, p.length()); ).sqrt() } } } impl Display for Point { fn fmt(&self, f: &mut Formatter) -> Result { write!(f, "({}, {}, {})", self.x, self.y, self.z) } }
main.rs use std::ops::*; pub struct Point3<T> { pub x: T, pub y: T, pub z: T } impl<T : Mul<Output = T> + Add<Output=T>> Point3<T> { pub fn new(x: T, y: T, z: T) -> Point3<T> { Point3 {x, y, z} } pub fn mult(&self, other:&Point3<T>) -> Point3<T> { Point3::new( self.x * other.x, self.y * other.y, self.z * other.z ) } pub fn add(&self, other:&Point3<T>) -> Point3<T> { Point3::new( self.x + other.x, self.y + other.y, self.z + other.z ) } }
main.rs fn main() { let operation = Operations::plus; } enum Operations { plus, minus, }
main.rs enum Command { Quit, Move { x: i32, y: i32 }, Write(String), ChangeColor(i32, i32, i32), }
main.rs fn main() { fn add_one(v : i32) -> i32 { v + 1 } println!("{}", add_one(4)); }
main.rs fn main() { fn add_one(v : i32) -> i32 { v + 1 } println!("{}", add_one(4)); }
main.rs fn main() { let x = 5; fn add_x(v : i32) -> i32 { v + x } println!("{}", add_x(4)); }
main.rs fn main() { let x = 5; let add_x = |v| v + x; println!("{}", add_x(4)); }
main.rs fn main() { panic!( “Smart pointers!" ); panic!( “Concurrency!" ); panic!( “Unit Tests" ); panic!( “Iterators" ); panic!( “Unsafe" ); panic!( “Object Oriented" ); panic!( “WASM" ); panic!( “Macros" ); }
main.rs fn main() { println!( “Thank you!" ); println!( “Your friend, Elemar Júnior!" ); println!( “falecom@elemarjr.co" ); println!( “@elemarjr" ); }
Recommend
More recommend