This file contains hidden or 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
// - - - - - - - - - - - - - - - - - - - - - - | |
fn get_word_count_from_file(file_name: &str) -> Result<u32, &str> { | |
//if the file is not found on the system, return error | |
return Err("File can not be found!") | |
//else, count and return the word count | |
//let mut word_count: u32; .... | |
Ok(word_count) | |
} | |
//💭 on above function, |
This file contains hidden or 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
// 01 ----------------------------------------- | |
// Adding methoids directly; without using traits | |
struct Player { | |
first_name: String, | |
last_name: String, | |
} | |
impl Player { | |
fn full_name(&self) -> String { |
This file contains hidden or 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
struct Player { | |
first_name: String, | |
last_name: String, | |
} | |
impl Player { | |
fn new(first_name: String, last_name: String) -> Player { | |
Player { | |
first_name : first_name, | |
last_name : last_name, |
This file contains hidden or 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
trait GetSound { | |
fn get_sound(&self) -> String; | |
} | |
struct Cat { | |
sound: String, | |
} | |
impl GetSound for Cat { | |
fn get_sound(&self) -> String { | |
self.sound.clone() |
This file contains hidden or 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
trait Person { | |
fn full_name(&self) -> String; | |
} | |
trait Employee : Person { //Employee inherit from person trait | |
fn job_title(&self) -> String; | |
} | |
trait ExpatEmployee : Employee + Expat { //ExpatEmployee inherit from Employee and Expat traits | |
fn additional_tax(&self) -> f64; |
This file contains hidden or 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
<?php | |
function solution($A) { | |
$cost = 0; | |
if (count($A) == 30) { | |
$cost += 25; | |
} else { | |
$ticketCount = [ | |
'1d' => 0, | |
'7d' => 0 | |
]; |
This file contains hidden or 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
fn main() { | |
println!("{}, {}!", "Hello", "world"); // Hello, world! | |
println!("{0}, {1}!", "Hello", "world"); // Hello, world! | |
println!("{greeting}, {name}!", greeting="Hello", name="world"); // Hello, world! | |
println!("{:?}", [1,2,3]); // [1, 2, 3] | |
println!("{:#?}", [1,2,3]); | |
/* | |
[ | |
1, |
This file contains hidden or 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
// Function | |
fn main() { | |
let x = 2; | |
println!("{}", get_square_value(x)); | |
} | |
fn get_square_value(x: i32) -> i32 { | |
x * x | |
} |
This file contains hidden or 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
fn main() { | |
let a = [1, 2, 3]; | |
let b = a; | |
println!("{:?} {:?}", a, b); // [1, 2, 3] [1, 2, 3] | |
} | |
fn main() { | |
let a = vec![1, 2, 3]; | |
let b = a; | |
println!("{:?} {:?}", a, b); // Error; use of moved value: `a` |