This is my first article about the Rust programming language, so I wanted to make it a little bit funny and interesting at the same time.
There are lots of great technical articles about Rust, but today we will talk about the keywords defined in Rust and languages that influenced the Rust creation.
Following Wikipedia there are languages that influenced Rust the most.
Rust was influenced by
Alef,C#, C++, Cyclone, Erlang, Haskell, Limbo, Newsqueak, OCaml, Ruby, Scheme, Standard ML, Swift
So, idea is to compare keywords defined in Rust with the keywords defined in “parent” languages and see the match rate and figure out the most “valuable” language. If you are interested you can find all keywords by languages mentioned in the article below.
All comparison result represents in the table, but we can make them more visual.
Keywords comparison table
“Parents” pie chart
“Parents” radar chart
As Java developer I had to include Java in comparison as well, so here we go.
“Parents” pie chart + Java
“Parents” radar chart + Java
Based on chart above and some observations gathered during comparison there are some fun facts:
C# and Swift most influences on Rust development and most critical “parents” of this language.
Erlang is still an important but less valuable “parent” for the Rust.
Rust’s keywords “self” and “Self” must be coming from Swift as this pair presents only there when “self” itself could be found in Swift, Ruby, and Limbo.
Even if Java and Rust share some common pieces there is not influence Java on Rust creation and development
There is no “goto” in Rust which everybody should avoid in Alef, C#, C++, Java.
Rust doesn’t have “new” present in C#, C++, OCaml, Java.
There is no “this” and “void” in Rust which you know by C#, C++, and Java
Rust’s “fn” highly possible goes from Limbo because only Limbo has the same keyword.
Rust’s “macro” must be going from Schema when “match” and “mod’ from OCaml.
Cyclone, Newsqueak, Standard ML are missed in analysis because it is quite difficult to find keywords for them.
Now, when we covered previous example this question should be easy for you.
Q: What value will be assigned to variable b in the code below?
public class Test{
public static void main(String[] args){
Boolean a = null;
boolean b = a;
}
}
A:no value will be assigned to b due to exception during execution.
Exception in thread "main" java.lang.NullPointerException
at Test.main(Test.java:4)
Code in the question is equal to this one
public class Test{
public static void main(String[] args){
Boolean a = null;
boolean b = a.booleanValue(); //null.booleanValue()
}
}
Q: What value will be assigned to variable c in the example below?
public class Test{
public static void main(String[] args){
Long a = 20L;
long b = 20;
boolean c = a.equals(b);
}
}
A:the correct answer – true.
public class Test {
public Test();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: ldc2_w #7 // long 20l
3: invokestatic #9 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
6: astore_1
7: ldc2_w #7 // long 20l
10: lstore_2
11: aload_1
12: lload_2
13: invokestatic #9 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
16: invokevirtual #15 // Method java/lang/Long.equals:(Ljava/lang/Object;)Z
19: istore 4
21: return
}
which is the same as
public class Test{
public static void main(String[] args){
Long a = 20L;
long b = 20;
boolean c = a.equals(Long.valueOf(b));
}
}
Q: What value will be assigned to variable c in the example below?
public class Test{
public static void main(String[] args){
Long a = 20L;
boolean c = a.equals(20);
}
}
A:the correct answer – false.
public class Test {
public Test();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: ldc2_w #7 // long 20l
3: invokestatic #9 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
6: astore_1
7: aload_1
8: bipush 20
10: invokestatic #15 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
13: invokevirtual #20 // Method java/lang/Long.equals:(Ljava/lang/Object;)Z
16: istore_2
17: return
}
In this example there is no type reference so 20 is represented as most appropriate integer type.
public class Test{
public static void main(String[] args){
Long a = 20L;
boolean c = a.equals(Integer.valueOf(20));
}
}
So, even if autoboxing/unboxing logic seems pretty straightforward and easy you still need to keep attention to details. As best practice to avoid memory and/or performance-related issues you shouldn’t rely on the autoboxing/unboxing mechanism in your code. The most appropriate case when this mechanism is really needed is when you store primitive types in the collection.