about
CompareTest
9/03/2022
Compare Test
organized bits of syntax, idoms, and patterns
Quick Status
Rust code bits | ||
---|---|---|
What is this? | Code demo | Commentary |
Hello World
The traditional first look at a new language |
|
In Rust, functions begin with the fn declarator, accept arguments in a parentisized list, encapsulate
an executable code in a set of statements enclosed in braces, and may declare a return type.
|
Hello Objects
Demonstrates how Types are defined and instances created. The type DemoObj is defined above its main function and instances dob and cdob are created in main. |
|
Types are defined with structs, like DemoObj on the left. Functionality is provided through methods,
e.g., functions associated with the struct. All methods are declared and defined in impl { ... } blocks.
The function DemoObj::new(&str) is a static method that creates an instance of DemoObj and endows it with a name.
Function name(&self) is a none-static method that accepts a reference to its instance and returns its name.
Static functions do not take a reference to self and so cannot access member data. They act like ordinary functions
but are accessed with their type followed by colons followed by the function. Non-static functions take a reference
to self, can access member data, and are accessed from an object name followed by a period.
Types and functions declare public accessibility with the pub keyword. Functions declare return types with
the -> SomeType syntax following the parameter list.
Macro names end with ! like the print!("..."); statement.
The code at the left declares the type and operations for DemoObj.
|
Hello Data | ||
Iteration | ||
Data Structures | ||
Functions | ||
Generic functions | ||
Structs | ||
Generic structs | ||
Basic DIP | ||
Generic DIP | ||