Rust Enums

Defining Enum Where structs give you a way of grouping together related fields and data, like a Rectangle with its width and height, enums give you a way of saying a value is one of a possible set of values. For example, we may want to say that Rectangle is one of a set of possible shapes that also includes Circle and Triangle. To do this, Rust allows us to encode these possibilities as an enum. ...

July 31, 2025 · 5 min · Kristian Alexander P

Rust Functions

Functions Functions are prevalent in Rust code. You’ve already seen one of the most important functions in the language: the main function, which is the entry point of many programs. You’ve also seen the fn keyword, which allows you to declare new functions. Rust code uses snake case as the conventional style for function and variable names, in which all letters are lowercase and underscores separate words. Here’s a program that contains an example function definition: ...

July 31, 2025 · 1 min · Kristian Alexander P

rust match

match Control flow construct Rust has an extremely powerful control flow construct called match that allows you to compare a value against a series of patterns and then execute code based on which pattern matches. Patterns can be made up of literal values, variable names, wildcards, and many other things. Think of a match expression as being like a coin-sorting machine: coins slide down a track with variously sized holes along it, and each coin falls through the first hole it encounters that it fits into. In the same way, values go through each pattern in a match, and at the first pattern the value “fits,” the value falls into the associated code block to be used during execution. ...

July 31, 2025 · 3 min · Kristian Alexander P

Rust Ownership

Understanding Ownership Ownership is Rust’s most unique feature and has deep implications for the rest of the language. It enables Rust to make memory safety guarantees without needing a garbage collector, so it’s important to understand how ownership works. In this chapter, we’ll talk about ownership as well as several related features: borrowing, slices, and how Rust lays data out in memory.

July 31, 2025 · 1 min · Kristian Alexander P

rust resources

Rust free and online resources Links https://www.youtube.com/@letsgetrusty

July 31, 2025 · 1 min · Kristian Alexander P

rust variables

Immutable By default rust variables are immutable. Once a value is bound to a name, you can’t change that value. We can make a mutable variable using the keyword mut. fn main() { let mut x = 5; println!("The value of x is: {x}"); x = 6; println!("The value of x is: {x}"); } The value of x is: 5 The value of x is: 6 Constant. Like immutable variables, constants are values that are bound to a name and are not allowed to change, but there are a few differences between constants and variables. First, you aren’t allowed to use mut with constants. Constants aren’t just immutable by default—they’re always immutable. You declare constants using the const keyword instead of the let keyword, and the type of the value must be annotated. We’ll cover types and type annotations in the next section, “Data Types”, so don’t worry about the details right now. Just know that you must always annotate the type. ...

July 31, 2025 · 3 min · Kristian Alexander P

programming

A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. Programming languages are used to create computer programs, which implement algorithms. Key characteristics and concepts of programming languages include: Syntax The set of rules that defines the combinations of symbols that are considered to be correctly structured statements or expressions in the language. Semantics The meaning of the instructions and how they are executed by the computer. ...

July 29, 2025 · 1 min · Kristian Alexander P

rust struct

Rust structs are similar to tuples, in that both hold multiple related values. Like tuples, the pieces of a struct can be different types. Unlike with tuples, in a struct you’ll name each piece of data so it’s clear what the values mean. Adding these names means that structs are more flexible than tuples: you don’t have to rely on the order of the data to specify or access the values of an instance. ...

July 29, 2025 · 1 min · Kristian Alexander P

perfect-number

What is perfect number? In number theory, a perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. For instance, 6 has divisors 1, 2 and 3 (excluding itself), and 1 + 2 + 3 = 6, so 6 is a perfect number. The next perfect number is 28, since 1 + 2 + 4 + 7 + 14 = 28. ...

March 14, 2024 · 1 min · Kristian Alexander P

python script examples

Example python scripts archlinux packages import subprocess def packages_list(): """Yay packages list""" packages = subprocess.run(["yay", "-Qu"], capture_output=True, text=True) if packages.stdout: return packages.stdout else: return "all packages updated!" return packages_list() get the wireless interface import subprocess import re def get_wlan_iface(): """Get the wireless interface name.""" interfaces = subprocess.run( ["ip", "link", "show"], capture_output=True, text=True ) pattern = r"(^\d+:\s+)(wl.+):" for iface in interfaces.stdout.splitlines(): if re.search(pattern, iface): match = re.search(pattern, iface) assert match is not None return match.group(2) return get_wlan_iface()

March 14, 2024 · 1 min · Kristian Alexander P