about
CompareTest
9/03/2022

Compare Test

organized bits of syntax, idoms, and patterns

Quick Status Recent page additions getting started testing UI format for code comparison
Rust code bits
What is this?Code demoCommentary
Hello World
The traditional first look at a new language
// HelloWorld::main.rs fn main() { println!("Hello, world!"); } 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. fn g(ArgType a) { ... } -> ReturnType If functions end with an expression, the value is returned; no return statement necessary. Those that end with statements, e.g., end with a semicolon, return the unit (), signifying nothing.
Each executable Rust program starts in a "main" function, which often calls other functions supplying operational details. There can be only one main in an executable.
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.
// CreateObject::main.rs /*------------------------------------- - Declare DemoObj struct, like C++ class - Rqst compiler impl traits Debug & Clone */ #[derive(Debug, Clone)] pub struct DemoObj { name : String } /*-- implement functions new and name --*/ impl DemoObj { pub fn new(obj_name: &str) -> DemoObj { DemoObj { name: obj_name.to_string() } } pub fn name(&self) -> &str { &self.name } } /*-- Demo DemoObj instance in action --*/ fn main() { print!( "\n -- demonstrate object creation --\n" ); let dob = DemoObj::new("Frank"); print!( "\n DemoObj instance name is {:?}", dob.name() ); let cdob = dob.clone(); print!( "\n name of clone is {:?}", cdob.name() ); print!("\n\n That's all Folks!\n\n"); } 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. C:\...\CreateObject\RustObject> cargo run -q -- demonstrate object creation -- DemoObj instance name is "Frank" name of clone is "Frank" That's all Folks!
Hello Data
Iteration
Data Structures
Functions
Generic functions
Structs
Generic structs
Basic DIP
Generic DIP
  Next Prev Pages Sections About Keys