Rust has a very powerful pattern matching system. You can match literals, structs, enums, slices (of varying lengths), specific fields in a struct (via de-constructing), nested fields, references, ranges, etc.

Assuming we have a fair understanding of pattern matching in Rust, let’s explore the ref keyword and the relationship between ref and & , when it comes to pattern matching.

Eg1:

let x:i32 = 1;
match x {
ref y => println!("{}", *y),
_ => ()
}

The above snippet is a very simple example (It’s obvious that the “default” condition will never get hit. But here we can ignore that, in the interest of this topic). Basically, ref comes into play after the match. i.e “Match and then treat the bound variable as a reference”. …


In the world of high level languages like Java, Python, JavaScript, Ruby, Go, the developer mindset is not really tuned to understand one important thing — which is Memory. The reason being — the Garbage Collector as it keeps track of all the objects in the memory for you. But this comes at cost of the GC running alongside your program. On the other end of the spectrum where you have C, C++ you can extract maximum performance out of your system, but then this comes at the cost of you having to manage the memory yourself. …


Let’s have a look at the following code:

trait Animal {
fn talk(&self);
}
struct Cat {}
struct Dog {}
impl Animal for Cat {
fn talk(&self) {
println!(“meow”);
}
}
impl Animal for Dog {
fn talk(&self) {
println!(“bark”);
}
}
fn animal_talk(a: &dyn Animal) {
a.talk();
}
fn main() {
let d = Dog {};
let c = Cat {};
animal_talk(&d);
animal_talk(&c);
}

We see that animal_talk()is invoked for both Catand Dog. But the function is not aware of the exact types. dyn tells the compiler to not determine the exact type and just be content with a reference to some type implementing the trait Animal. …


When it comes to Kubernetes networking, Calico is widely used. One of the main reasons being its ease of use and the way it shapes up the network fabric. Calico is a pure L3 solution, where packets are routed in just the same manner as your regular Internet. Each node (eg. VM) acts like a vRouter, which means tools like traceroute, ping, tcpdump, etc just work as expected! Whether the packet is flowing from one container to another or container to another node (or vice-versa), its just treated as a flat network route (L3 hops). By default, there is no notion of overlays, tunneling or NAT. Each endpoint is actually a /32 IP in IPv4 (or equivalent in other), which means a container can be assigned a public IP. All this is achieved using the Linux kernel’s existing network capabilities. …


Before moving to understanding the mutable reference, let’s have a look at a very basic example in C.

int main() {
int i = 1;
i = 2;
}

This code compiles just fine. Let’s see its Rust equivalent.

fn main() {
let i:i32 = 1;
i = 2;
}

This fails with error:

error[E0384]: cannot assign twice to immutable variable `i`
--> src/main.rs:3:5
|
2 | let i = 20;
| - first assignment to `i`
3 | i = 2;
| ^^^^^ cannot assign twice to immutable variable

Rust treats its variables as immutable, unless specified like this:

let mut i:i32 =…


CNI is being mentioned a lot in conjunction with Kubernetes. It is now become an integral part of Kubernetes, silently doing it’s job of connecting the pods across different nodes and being well adaptive in integrating different kind of network solutions (overlays, pure L3, etc). Having said that, it is not only present in Kubernetes. It in itself is a separate framework with it’s own specification and can be used not only with Kubernetes but also with other container orchestrators like Mesos, Openshift, etc.

So what is CNI? To put it succinctly, it is an interface between a network namespace and network plugin. A container runtime (eg. Docker, Rocket) is a network namespace. Network Plugin is the implementation that follows the CNI specification to take a container runtime and configure (attach, detach) it to the network, that the plugin implements. …


Having a dynamic public IP can be really annoying when it comes to accessing the services in AWS, guarded by security groups (assuming you are a small shop and don’t have the VPN or AWS DirectConnect infra setup yet or maybe it is your personal AWS account.)

Anyway, it’s a bit tiring to open the browser, load AWS console , click on the security groups, edit it and then save it back. All this, so that you can ssh into your EC2 instance, for example. The worst is when AWS console has signed you out (due to inactivity) and then you have to wait for a very very long time on the sign-in to complete. …


Here, we will cover a very simple and basic topic in Rust. The relation between reference (&) and Copy trait.

What happens above is that struct Cat is passed to thetest function. This compiles fine. Now let’s what happens if I add one more call to the functiontest:

error[E0382]: use of moved value: `c`
--> src/main.rs:11:10
|
10 | test (c);
| - value moved here
11 | test (c);
| ^ value used here after move
|
= note: move occurs because `c` has type `Cat`, which does not implement the `Copy` trait

The error is pretty simple, telling you that you cannot use a moved variable, since Cat does not have the Copy trait, as evident from the Cat’s definition. The value got moved in the first call to the function test. Note that instead of using a function, the same can also be demonstrated like…

About

vikram fugro

Open Source Software Enthusiast, Polyglot & Systems Generalist.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store