Uploaded by Nerea De Vicente

ASSIGNMENT 1

advertisement
ZIG
The first language I will be writing about is Zig.
This language was appeared in 2015 and was created by Andrew Kelley.
The focus of this open and non-profit programming language is having
general use, maintaining the software in optimal conditions and facilitating
its reuse. The language appeared as a more efficient alternative to C and with
a higher performance. It is mainly aimed for systems programming.
What makes Zig special is its great efficiency and portability compared with
other languages. Also, its aim is to amend the limitations and obstacles that
other languages might have. In addition, it has new and promising aspects
such as the execution of code while compiling.
Lets see an example of Zig code:
const std = @import("std");
const MyError = error{
GenericError
};
fn sub(val: i32) !i32 {
if (val == 42) return MyError.GenericError;
return val;
}
pub fn main() !void {
_ = sub(42) catch |err| {
std.debug.print("Wrong: {}\n", .{err});
};
std.debug.print("end!", .{});
}
This code in Zig defines a customized error type called "MyError". Then, it
has a "sub" function that takes a number and returns that number if it is not
equal to 42; otherwise, it returns a MyError.GenericError error.
In the main function, sub(42) is called. If sub returns an error, it captures it
and prints it as "Wrong". Then, regardless of whether there is an error or not,
it prints "end!".
In summary, the program checks if a number is equal to 42 and handles
custom errors using MyError, then prints an "end!" message.
It is interesting to look at this program because here we can see some of the
characteristics of Zig. For example, this program shows that zig allows to
represent and handle errors in a more structured and readable way. This can
be especially useful in larger and more complex programs. Along with this
we can see that Zig handles errors with the catch keyword. This feature
allows you to catch and handle errors in an efficient and specific way,
making it easy to handle errors in a program.
It is still in use today and in fact, based on a survey from Stack Overflow it
is one of the best paid languages nowadays.
RUST
Rust began in 2006 as a side project of Graydon Hoare, a Mozilla employee.
Mozilla saw the potential of the new language and began sponsoring it in
2009, before revealing it to the world in 2010. The first stable version of Rust
was released on May 15, 2015.
It was created to achieve c velocity without memory problems. For this
reason it is considered its successor.
I decided to choose a Rust code fragment where we could see error handling
so we could compare it to Zig.
fn drink(beverage: &str) {
// You shouldn't drink too much sugary beverages.
if beverage == "lemonade" { panic!("AAAaaaaa!!!!"); }
println!("Some refreshing {} is all I need.", beverage);
}
fn main() {
drink("water");
drink("lemonade");
}
This Rust fragment defines a function called drink that takes a drink as an
argument. If drink is equal to "lemonade", it checks if the panic
compilation feature is set to "abort". Depending on this setting, it prints a
different message: "This is not your party. Run!!!!" if panic is "abort", or
"Spit it out!!!!" if it is not. If the drink is not "lemonade", it simply prints
"Some refreshing {drink} is all I need." where {drink} is replaced by the
name of the drink. In the main function, drink is called twice, once with
"water" and once with "lemonade", which produces different outputs
depending on the drink and the panic setting.
What I find most interesting about this fragment is that we can see the way
Rust uses the cfg! macro to make compile-time decisions and shows how to
customize the output based on these conditions.
Rust is used by many programmers and indeed its usage was nearly tripled
in two years to over 2.2 million users by the first quarter of 2022.
Download