Skip to content

Instantly share code, notes, and snippets.

View dumindu's full-sized avatar
🦄
One step at a time

Dumindu Madunuwan dumindu

🦄
One step at a time
View GitHub Profile
// - - - - - - - - - - - - - - - - - - - - - -
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,
// 01 -----------------------------------------
// Adding methoids directly; without using traits
struct Player {
first_name: String,
last_name: String,
}
impl Player {
fn full_name(&self) -> String {
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,
trait GetSound {
fn get_sound(&self) -> String;
}
struct Cat {
sound: String,
}
impl GetSound for Cat {
fn get_sound(&self) -> String {
self.sound.clone()
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;
<?php
function solution($A) {
$cost = 0;
if (count($A) == 30) {
$cost += 25;
} else {
$ticketCount = [
'1d' => 0,
'7d' => 0
];
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,
// Function
fn main() {
let x = 2;
println!("{}", get_square_value(x));
}
fn get_square_value(x: i32) -> i32 {
x * x
}
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`
fn main() {
let a = [1, 2, 3];
let b = &a;
println!("{:?} {}", a, b[0]); // [1, 2, 3] 1
}
fn main() {
let a = vec![1, 2, 3];
let b = get_first_element(&a);