Comprehensive documentation of ShitRust's syntax, types, functions, and standard library.
ShitRust's syntax is designed to be familiar to developers coming from Rust, Python, or C/C++. It emphasizes readability while maintaining expressiveness.
// This is a single-line comment
/*
This is a
multi-line comment
*/
/// Documentation comment for functions, structs, etc.
/// Supports markdown formatting
ShitRust features a static type system with type inference. The following are the primary built-in types:
int - Signed integer (default is platform-dependent, typically 32 or 64-bit)float - Floating-point number (default is 64-bit)bool - Boolean type with values true and falsechar - Single Unicode characterstring - UTF-8 encoded string// Type inference
let x = 5; // x is inferred as int
let name = "Alice"; // name is inferred as string
// Explicit type annotations
let y: float = 3.14;
let active: bool = true;
// Type aliases
type UserId = int;
let user_id: UserId = 1001;
An expression in ShitRust evaluates to a value. Most constructs in ShitRust are expressions.
// Integer literals
42 // Decimal
0x2A // Hexadecimal
0b101010 // Binary
0o52 // Octal
// Float literals
3.14
2.71e2 // Scientific notation
// Boolean literals
true
false
// Character literal
'A'
// String literals
"Hello, World!"
"Line 1\nLine 2" // With escape sequences
// Multiline strings
"""
This is a multiline
string in ShitRust
"""
+ - Addition- - Subtraction* - Multiplication/ - Division% - Modulo (remainder)** - Exponentiation== - Equal to!= - Not equal to< - Less than> - Greater than<= - Less than or equal to>= - Greater than or equal to&& - Logical AND|| - Logical OR! - Logical NOTStatements are instructions that perform some action but don't return a value.
// Immutable variable declaration
let x = 5;
// Mutable variable declaration
let mut y = 10;
// Constants
const MAX_ITEMS = 100;
// If statement
if condition {
// Code executed if condition is true
} else if another_condition {
// Code executed if another_condition is true
} else {
// Code executed if no conditions are true
}
// Match statement
match value {
pattern1 => expression1,
pattern2 => expression2,
_ => default_expression,
}
// While loop
while condition {
// Loop body
}
// For loop
for item in collection {
// Loop body
}
// Range-based for loop
for i in 0..10 {
// Loop body, i takes values 0 through 9
}
Functions in ShitRust are defined using the fn keyword.
// Basic function
fn add(a: int, b: int) -> int {
return a + b;
}
// Function with no return value (void)
fn print_hello() -> void {
println("Hello!");
}
// Function with default parameters
fn greet(name: string, greeting: string = "Hello") -> string {
return greeting + ", " + name + "!";
}
// Function with variadic parameters
fn sum(...numbers: int) -> int {
let total = 0;
for n in numbers {
total = total + n;
}
return total;
}
// Function with early return
fn is_even(num: int) -> bool {
if num % 2 == 0 {
return true;
}
return false;
}
Structs are used to create custom data types that group related values together.
// Struct definition
struct Point {
x: float,
y: float,
// Method
fn distance_from_origin() -> float {
return (self.x * self.x + self.y * self.y).sqrt();
}
// Static method (constructor)
fn new(x: float, y: float) -> Point {
return Point { x: x, y: y };
}
}
// Creating an instance
let p1 = Point { x: 3.0, y: 4.0 };
let p2 = Point::new(5.0, 12.0);
// Accessing fields
let x_coord = p1.x;
// Calling methods
let distance = p1.distance_from_origin();
Modules are used to organize code into logical units and control visibility (public vs. private).
// Defining a module
module math {
// Public function (can be accessed outside the module)
pub fn add(a: int, b: int) -> int {
return a + b;
}
// Private function (only accessible within the module)
fn subtract(a: int, b: int) -> int {
return a - b;
}
// Nested module
module advanced {
pub fn power(base: int, exponent: int) -> int {
return base ** exponent;
}
}
}
// Importing modules
import math;
import math.advanced;
// Using imported modules
let sum = math.add(5, 3);
let pow = math.advanced.power(2, 8);
ShitRust provides multiple mechanisms for error handling: try/catch blocks and Result types.
try {
// Code that might throw an error
let result = risky_operation();
} catch err {
// Code that handles the error
println("Error: " + err.to_string());
} finally {
// Code that always runs, whether there was an error or not
cleanup_resources();
}
// Function returning a Result type
fn divide(a: int, b: int) -> Result {
if b == 0 {
return Err("Division by zero");
}
return Ok(a / b);
}
// Using the Result
let result = divide(10, 2);
if result.is_ok() {
println("Result: " + result.unwrap().to_string());
} else {
println("Error: " + result.unwrap_err());
}
// Using the ? operator for error propagation
fn calculate() -> Result {
let a = divide(10, 2)?; // Returns early if error
let b = divide(20, a)?; // Returns early if error
return Ok(b);
}