Rust - Getting Started (May. 2021. updating continuously)

What’s this page? As of Jan. 2021, I work as a DevOps (or infrastructure) engineer, but I like to solve problems with codes (front, back, whatever. it depends on the purposes.) For a year, my motivation abour learning Rust surges enough. This page is a memo while I’ve learned with Rust official document so that I can easily reminde the key feafures of Rust. Most part of this post consist of quotes from the document, but I also leave my opinions (could be wrong.

Basic sort algorithms in Rust

Disclamer The implementations in this post has a lot of room to improvement. As of Apr. 2021, they are more like code monkey’s code :( Reverse a string fn main() { let s1 = String::from("Please reverse me with spaces!日本語.한국어"); println!("Original string: {}",s1); let mut s2 = String::from(""); for c in s1.chars().rev() { s2.push(c); } println!("Reversed: {}",s2); } In Rust, both String and a string slice &str are UTF-8 encoded.

Custom configuration, and inject JavaScript in posts

Motivation To enable TikZ in my note, I add a header in layout/partial/header.html at first. But this change injects the javascript lines into all my posts which don’t need to include the TikZ. So I want to inject JavaScript in case that I mark the post as “TikZ enabled”. How to Create a HTML template In layouts/partials/header.html. {{ if and .IsPage (eq (.Param "tikz") true) }} <link rel="stylesheet" type="text/css" href="https://tikzjax.com/v1/fonts.css"> <script src="https://tikzjax.

Category Theory (Apr.2021. updating continuously)

Materials Category Theory by Steve Awodey Very kind “Category Theory For Beginners” by Richard Southwell. Definition of a category Objects and arrows are there. There should be identity arrows. Arrows should be composited. Associativity holds. Terminology: domain and codomain $\mathrm{dom}(f)\xrightarrow{f} \mathrm{cod}(f)$ Functor A Functor is a mapping between categories. It’s like an arrow from category to category. It preseves compositions $F(g\cdot f) = F(g)\cdot F(f)$. Identitiy arrows are mapped to identity arrows.

Use Decorator in Python

Let’s grasp the concept of decorator in Python PEP 318 with short snippets. Step 1: Function as a parameter def f1(): print("Called f1.") def f2(f): f() f2(f1) # Called f1 f2 takes a function (object) as a parameter. Step 2: Wapping function def f1(fun): def wrap(): print("Start wrap") fun() print("End wrap") return wrap def f(): print("In function f") f1(f)() ### python test.py #Start wrap #In function f #End wrap Step 3: Use decorator def f1(fun): def wrap(): print("Start wrap") fun() print("End wrap") return wrap @f1 def f(): print("In function f") f() ### python test.

Rust Serverless

Getting started I followed the link. In this page, the author wrote details and it’s very educational. To remind the procedure, I left memo. I really appreciate you, Chuma Umenze :) Hello world code cargo new rust_lambda Cargo.toml: [package] // --- snip --- autobins = false [dependencies] lambda_runtime = "0.2.1" serde = { version = "^1.0", features = ["derive"] } [[bin]] name = "bootstrap" path = "src/main.rs" src/main.rs: use lambda_runtime::{error::HandlerError, lambda, Context}; use serde::{Deserialize, Serialize}; use std::error::Error; #[derive(Deserialize, Serialize, Clone)] #[serde(rename_all = "camelCase")] struct LambdaRequest { full_name: String, message: Option<String>, } #[derive(Deserialize, Serialize, Clone)] #[serde(rename_all = "camelCase")] struct LambdaResponse { lambda_request: LambdaRequest, } fn main() -> Result<(), Box<dyn Error>> { lambda!

Quick Ansible test

This is memo when I want to run simple tasks quickly. Directory. $ tree . ├── ansible.cfg ├── hosts └── test.yml In hosts. [servers] server1.mydomain.com server2.mydomain.com server3.mydomain.com test.yml. - name: test-playbook hosts: servers tasks: - cron: name: "Add a cron when rebooting" special_time: reboot job: "echo $PATH" user: atlex00 ansible.cfg [defaults] host_key_checking = False deprecation_warnings=False Exec: ansible-playbook ./test.yml -i hosts -bD -C

NVM

What’s nvm Node.js Version Manager. Like pyenv in Python. Different from Python, you can’t label a name to the environment. package.json manage the libraries. Install nvm Follow the official document. https://github.com/nvm-sh/nvm $ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash ..... (simple results) $ export NVM_DIR="$HOME/.nvm" $ [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm $ [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion Check.

Serverless framework - Getting Started with Python

I tried Serverless framework with Python AWS Lambda. Prerequirement Node.js and npm. $ node -v v10.19.0 $ npm -v 7.5.2 Install I created my serverless account with Google SSO. sudo npm install -g serverless Hello world Create a project: $ serverless Serverless: No project detected. Do you want to create a new one? Yes Serverless: What do you want to make? AWS Python Serverless: What do you want to call this project?

Small tip: Multi aws-cli environment

Motivation If you use multi AWS accounts in your work environment, I hightly recommend to configure your “aws-cli profile” so that you can easily change your aws-cli account. The only thing you need to do when you want to change your aws-cli environment is the option --profile. Very simple. How to configure https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html Check under ~/.aws/. You can configure multi profile.