2020-02-09
Table of contents:
2021-03-07
This document gives an overview about the programming language rust. Especially the wording, the syntax. If you come from a different programming language then you may need a kind of mind mapping.
Learn Rust from my perspective.
cargo is the build system and package manager. Dependencies will be automatically downloaded.
A cargo project is defined by a cargo.toml
file.
The command for building a cargo project
cargo build
To run a cargo project
cargo run
crates are libraries
Rust manages memory with ownership system. While some languages clean up unused memory automatically (gargabe collector) or require programmers (allocate and free) to do it. Each value has an owner and you can borrow it.
modules are used for code organizing
attributes are annotations
A
trait
is a collection of methods defined for an unknown type:Self
So in other words then the official description. A trait
is a interface
in java language.
We know the data type struct
from C language. Rust provides in addition two more struct types: tuples and unit structs.
There is no class type in Rust. In Go language we also don't have class types.
You defined a structure but also want to add methods. Then you can use the impl
keyword.
struct Developer {
exp: u8
}
impl Developer {
fn isExpert(&self) -> bool {
self.exp > 100
}
}
Text can stored in rust in two ways.
The first way is to use the 'String' type. Which is a struct data type defined in rust std library: Struct std::string::String
. The String
is stored as a vector of bytes Vec<u8>
.
And there is also a second string data type: &str
.
A slice is a two word object. The first word is a pointer to the data. The second word is the length of the slice.
pointer size is usize (pointer size).
usize
is determined by the processor architecture. e.g. onx86-64
the usize is 64 bit.
A quick refresher regarding word:
8 bit = 1 byte
2 byte = 1 word
1 dword (double word) = 2 word
Rust has in-build support for working with streaming or functional programming or collection tranformation operations.
A basic example. We have a vector of numbers and want to display that data as a reversed string. First we need a iterator [iter()
]. Afterwards we can begin our data transformation.
let data = vec![0, 1, 1, 1];
let s: String = data.iter().map(|d| d.to_string()).rev().collect();
// output: 1110
You may ask what is
vec!
. It's a macro, an alias to create newVec
and initialize with values.
2021-03-07
Go has no class types. Instead you can use struct
.
type Developer struct {
experience int
}
If you want to add a method to a class then you can define a function with a receiver argument.
func (d Developer) Display() void {}
2021-03-07
This document gives an overview of Kotlin programming languages characteristic components.
Kotlin is a statically typed programming language like Java, C++, etc. Means that type of a variable is known at compile time. Whereas dynamically typed programming languages like Python, JavaScript variable types are known at runtime.
When you write Kotlin code .kt
the Kotlin compiler creates a .class
file. The compiler and the final application need the Kotlin runtime library.
Java development speed increased in latest years.
But currently there are still 3rd party libraries like lombok that developers prefer to get rid of boilerplate code. This brings new dependency and issues for later maintanence difficulities. Kotlin can also interact with Lombok generated code since 1.5.20.
Instead of using 3rd party libraries a developer may switch to kotlin. The interoperability with java code works well. In some edge-case it may have difficulities. But instead of introduce a new 3rd party the developer should use either java features only or introduce kotlin to an existing project.
A good example in the described example are data class
in kotlin. In Java there will come a new feature record
(see link [1]). With Lombok you can achieve same result as a data class.
An Enumartion data type is in Kotlin a class. It defined by enum class
. Similar to sealed class
.
Example: we define three constants. Whereas each constant is an object of the defined enum class.
enum class Developer {
JUNIOR, SENIOR, EXPERT
}
Lets continue the example and give the option to assign an experience value. Each enum (e.g. EXPERT
) is an instance of the defined enum class and initialized with the given experience
value.
enum class Developer(val experience: Int) {
JUNIOR(1), SENIOR(10), EXPERT(100)
}
As the name sealed suggest a sealed class
is restricted. Restricted means subclasses are only allowed at compile-time. A sealed class is also abstract.
A Sealed class can more then an enum class.
by lazy
follows the delegation pattern. In the following example a get operation will be delegated to the String and only computed on first access.
val code: String by lazy {
"kotlin"
}
When should we use by lazy
property initialization?
A good case is if you have a heavy class which initialization is time-consuming. Wouldn't is be better if we create it when it's needed? Another benefit is you will always use the same instance once an instance is created.
class Developer {
private val coffee: CoffeeGrinder by lazy {
CofeeGrinder()
}
}
With lateinit
you also achieve a later initialization like by lazy
. In in this case you define where it's initialized.
lateinit
is good if you want to prevent nullable variable.
val data: String? = null
Better use lateinit
.
lateinit var data: String
If you have a nullable variable and want to ensure an alternative or default value you can use the
?:
aka Elvis operator.
val process = data?.payload ?: "default-payload"
Pair
println("# collections: pair")
val j = Pair("java", 11)
log("first: ${j.first}; second: ${j.second}")
// with _syntactic sugar_
val k = "kotlin" to 1.4
log("first: ${k.first}; second: ${k.second}")
Triple
val green = Triple(0, 255, 0)
log("rgb: ${green.first}, ${green.second}, ${green.third}")
Array
val i = intArrayOf(0, 10, 200)
val n = arrayOfNulls<Int>(3)
List
val n = listOf(0, 1, 2, 3, 3);
Map
val stack = mutableMapOf<Int, String>(10 to "python", 11 to "rust")
Set
val binary = setOf(0, 1, 1, 1)
2020-02-09
#include <iostream>
using namespace std;
int main() {
float* p;
p = new float(3.14);
cout << "\nnew example: " << *p;
int size = 8;
int* a = new int[size];
cout << "\narray: " << *a;
*a = 100;
*a++;
*a = 200;
*a--;
cout << "\narray: " << *a;
cout << "\narray: " << a[1] << endl;
delete [] a;
cout << "\nprocessing finished: " << a[0] << endl;
cout << "\nprocessing finished: " << endl;
cout << "\nsegmentation fault: " << a[100] << endl;
}