K

@kenbitanga

Rust: Vector of Structs, Owned String, Borrowed String

Rust
2 years ago
struct Person { name: String, age: u32 } // &str is borrowed string fn print_person(name: &str) { print!("{}, ", name); }

JS: Promise Example

NodeJS
2 years ago
let isShopOpen = true; let order = (time, work) => { return new Promise((resolve, reject) => { if (isShopOpen) { setTimeout(() => { resolve(work()); }, time); } else { reject(console

Go: Interface

Go
2 years ago
package main import "fmt" type Animal interface { feed() } type Dog struct { name string } func (d *Dog) feed() {

Rust: Read & validate user input

Rust
2 years ago
fn main() { println!("What is your age?"); let mut age = String::new(); std::io::stdin() .read_line(&mut age) .expect("Error"); let age: u32 = match age.trim().parse() {

Go: Interface, Runtime Polymorphism

Go
2 years ago
package main import "fmt" type Animal interface { feed() } type Dog struct { name string

Go: Slice - Sum - Average

Go
2 years ago
package main import "fmt" func sum(values []int) int { sum := 0 for _,v := range values { sum += v } return sum

Rust: Factorial

Rust
2 years ago
fn fact(n: u32) -> u32 { if n < 2 { return 1; } return n * fact(n - 1); } fn main() { for i in 0..11 { println!("{}! = {}", i, fact(i)); } }

Rust: Fibonacci

Rust
2 years ago
fn fib(n: u32) -> u32 { if n == 0 { return 0; } if n <= 2 { return 1; } return fib(n - 1) + fib(n - 2); } fn main() { for i in 0..11 { println!("fib({}) = {}", i, fib(i)); } }

Rust: String, &str

Rust
2 years ago
fn main() { let mut string = String::new(); // append a char string.push('a'); // append a &str string.push_str("bcde"); // convert String to &str

Rust: Read user input

Rust
2 years ago
fn main() { println!("What's your name?"); // create an empty growable String object let mut name = String::new(); // read user input std::io::stdin() .read_line(&mut name) .expect("Read line error");

Rust: Guess the number!

Rust
2 years ago
use std::io; use rand::Rng; use std::cmp::Ordering; fn main() { println!("Guess the number!"); // generate random number from 1 to 100 let secret_number = rand::thread_rng().gen_range(1..=100);

Rust: Fibonacci - If Else If - For Loop - Range

Rust
2 years ago
fn fib(n: i32) -> i32 { if n < 1 { return 0; } else if n < 3 { return 1; } else { return fib(n - 1) + fib(n - 2); } } fn main() {

Rust: Strings

Rust
2 years ago
fn pass_str(s: &str) -> &str { return s; } fn pass_string(s: String) -> String { return s; } fn main() { // using string literal let test: &str = "Hello world!"; println!("{}", pass_str(test));

Go: Methods - Getters - Setters

Go
2 years ago
package main import "fmt" type Point struct { x int y int } func (p *Point) get() string { return fmt.Sprintf("Point(%v, %v)", p.x, p.y)

Rust: Constructors & Methods

Rust
2 years ago
struct Point { x: f64, y: f64, } impl Point { // associated function // generally used as constructor fn origin() -> Point { Point { x: 0.0, y: 0.0 } }

Rust: Tuples - Destructuring

Rust
2 years ago
fn main() { // create a tuple let tup = ("jake", 23); // destructure a tuple let (name, age) = tup; println!("{:?}", tup); println!("{} is {} years old", name, age); }

Python: Sigma Formula

Python
2 years ago
def sigma(n): res = n * (n + 1) / 2 # = (n + 1) * n / 2 # = (n * n / 2) + (n / 2) return int(res) for i in range(5): print('sigma({}) = {}'.format(i, sigma(i)))

Python: Factorial - For Loop

Python
2 years ago
def fact(n): if n < 0: return 'undefined' res = 1 for i in range(2, n+1): res *= i return res for i in range(-1, 5): print('{}! = {}'.format(i, fact(i)))

Python: Factorial - Recursion

Python
2 years ago
def fact(n): if n < 0: return 'undefined' if n <= 1: return 1 return n * fact(n-1) for i in range(-1, 7): print("{}! = {}".format(i, fact(i)))

Python: Factorial - Recursion

Python
2 years ago
def fact(n): if n < 0: return 'undefined' if n <= 1: return 1 return n * fact(n-1) for i in range(-1, 7): print("{}! = {}".format(i, fact(i)))