first commit
This commit is contained in:
@@ -0,0 +1,286 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; font-src 'self' data:; img-src 'self' data:; connect-src 'self'; base-uri 'self'; form-action 'self' https://defcon.social https://bsky.app;">
|
||||
<meta http-equiv="X-Content-Type-Options" content="nosniff">
|
||||
<link rel="stylesheet" href="../assets/css/style.css">
|
||||
<link rel="icon" type="image/x-icon" href="../favicon.ico">
|
||||
<script>
|
||||
// Apply theme immediately to prevent flash
|
||||
(function() {
|
||||
const theme = localStorage.getItem('theme') ||
|
||||
(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
|
||||
document.documentElement.setAttribute('data-theme', theme);
|
||||
})();
|
||||
</script>
|
||||
<title>Bash Quickstart Reference - Quick Reference - Launch Pad</title>
|
||||
</head>
|
||||
<body>
|
||||
<button class="theme-toggle" id="themeToggle" aria-label="Toggle dark mode">
|
||||
<svg class="theme-icon theme-icon-moon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path></svg>
|
||||
<svg class="theme-icon theme-icon-sun" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display: none;"><circle cx="12" cy="12" r="5"></circle><line x1="12" y1="1" x2="12" y2="3"></line><line x1="12" y1="21" x2="12" y2="23"></line><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line><line x1="1" y1="12" x2="3" y2="12"></line><line x1="21" y1="12" x2="23" y2="12"></line><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line></svg>
|
||||
</button>
|
||||
<br/><br/>
|
||||
<div class="name">
|
||||
__ _______________________ _________._________________________
|
||||
/ \ / \_ _____/\______ \/ _____/| \__ ___/\_ _____/
|
||||
\ \/\/ /| __)_ | | _/\_____ \ | | | | | __)_
|
||||
\ / | \ | | \/ \| | | | | \
|
||||
\__/\ / /_______ / |______ /_______ /|___| |____| /_______ /
|
||||
\/ \/ \/ \/ \/
|
||||
|
||||
</div>
|
||||
<div class="blog-page-header">
|
||||
<div class="blog-header-content">
|
||||
<a href="/quickref" class="back-link" title="Back to Quick Reference">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="42" height="42" viewBox="0 0 24 24" class="home-icon"><path fill="currentColor" d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/></svg>
|
||||
</a>
|
||||
<h1 class="blog-page-title">Bash Quickstart Reference</h1>
|
||||
</div>
|
||||
</div>
|
||||
<div class="blog-post-container">
|
||||
<div class="blog-posts-container" style="max-width: 900px; margin: 0 auto;">
|
||||
<div class="blog-post">
|
||||
<div class="blog-post-content">
|
||||
<p><a href="index.html">← Back to quick reference</a></p>
|
||||
<p><a href="../index.html">← Home</a></p>
|
||||
<hr>
|
||||
<p>Quick reference guide for Bash. Essential syntax, control flow, variables, arrays, and common patterns for shell scripting.</p>
|
||||
<hr>
|
||||
<h2>Hello World</h2>
|
||||
<pre><code>#!/bin/bash
|
||||
echo "Hello, World!" # The shebang tells the system which interpreter to use</code></pre>
|
||||
<hr>
|
||||
<h2>Variables</h2>
|
||||
<pre><code>name="Alice" # No spaces around =; quotes protect spaces
|
||||
x=42 # Numbers are strings until you do math
|
||||
readonly PI=3.14 # Make variable read-only
|
||||
unset name # Delete variable
|
||||
|
||||
echo $name # Access with $ prefix
|
||||
echo ${name} # Braces are safer for variable expansion
|
||||
echo "${name}_suffix" # Use braces when concatenating</code></pre>
|
||||
<hr>
|
||||
<h2>Control Flow</h2>
|
||||
<h3>If/Else</h3>
|
||||
<pre><code>if [ $x -gt 10 ]; then # [ ] requires spaces; -gt means "greater than"
|
||||
echo "x is big"
|
||||
else
|
||||
echo "x is small"
|
||||
fi
|
||||
|
||||
# Comparison operators: -eq, -ne, -lt, -le, -gt, -ge
|
||||
# String comparison: =, !=, -z (empty), -n (non-empty)
|
||||
|
||||
if [ -f "file.txt" ]; then # -f checks if file exists
|
||||
echo "File exists"
|
||||
elif [ -d "dir" ]; then # -d checks if directory exists
|
||||
echo "Directory exists"
|
||||
fi</code></pre>
|
||||
<h3>For Loops</h3>
|
||||
<pre><code>for i in 1 2 3 4 5; do
|
||||
echo $i
|
||||
done
|
||||
|
||||
for i in {1..5}; do # Brace expansion: 1 to 5
|
||||
echo $i
|
||||
done
|
||||
|
||||
for file in *.txt; do # Iterate over files
|
||||
echo "Processing $file"
|
||||
done
|
||||
|
||||
for ((i=0; i<5; i++)); do # C-style for loop
|
||||
echo $i
|
||||
done</code></pre>
|
||||
<h3>While Loops</h3>
|
||||
<pre><code>while [ $x -gt 0 ]; do
|
||||
echo $x
|
||||
x=$((x - 1)) # Arithmetic expansion
|
||||
done
|
||||
|
||||
# Read file line by line
|
||||
while IFS= read -r line; do
|
||||
echo "$line"
|
||||
done < file.txt</code></pre>
|
||||
<h3>Case Statement</h3>
|
||||
<pre><code>case $choice in
|
||||
"1")
|
||||
echo "Option 1"
|
||||
;;
|
||||
"2"|"two")
|
||||
echo "Option 2"
|
||||
;;
|
||||
*)
|
||||
echo "Default case"
|
||||
;;
|
||||
esac</code></pre>
|
||||
<hr>
|
||||
<h2>Functions</h2>
|
||||
<pre><code>function greet() {
|
||||
local name=$1 # $1 is first argument; local limits scope
|
||||
echo "Hello, $name!"
|
||||
}
|
||||
|
||||
greet "Alice" # Call function
|
||||
|
||||
# Alternative syntax
|
||||
add() {
|
||||
local a=$1
|
||||
local b=$2
|
||||
return $((a + b)) # Return exit code (0-255)
|
||||
}
|
||||
|
||||
add 2 3
|
||||
result=$? # $? is exit code of last command</code></pre>
|
||||
<hr>
|
||||
<h2>Arrays</h2>
|
||||
<pre><code>fruits=("apple" "banana" "cherry") # Declare array
|
||||
fruits[3]="date" # Add element
|
||||
echo ${fruits[0]} # Access element (apple)
|
||||
echo ${fruits[@]} # All elements
|
||||
echo ${#fruits[@]} # Array length
|
||||
|
||||
for fruit in "${fruits[@]}"; do # Iterate over array
|
||||
echo $fruit
|
||||
done</code></pre>
|
||||
<hr>
|
||||
<h2>Strings</h2>
|
||||
<pre><code>text="Hello, World!"
|
||||
echo ${#text} # String length
|
||||
echo ${text:0:5} # Substring: start at 0, length 5 ("Hello")
|
||||
echo ${text/World/Bash} # Replace first match ("Hello, Bash!")
|
||||
echo ${text//l/L} # Replace all matches
|
||||
echo ${text#Hello, } # Remove shortest prefix match
|
||||
echo ${text##Hello, } # Remove longest prefix match
|
||||
echo ${text% World!} # Remove shortest suffix match
|
||||
echo ${text%% World!} # Remove longest suffix match</code></pre>
|
||||
<hr>
|
||||
<h2>Input / Output</h2>
|
||||
<pre><code>read name # Read user input
|
||||
echo "Enter your name: "
|
||||
read -p "Name: " name # Prompt and read in one line
|
||||
read -s password # Silent input (for passwords)
|
||||
read -a array # Read into array
|
||||
|
||||
echo "Hello" # Print to stdout
|
||||
echo -n "No newline" # -n suppresses newline
|
||||
printf "Value: %d\n" 42 # Formatted output</code></pre>
|
||||
<hr>
|
||||
<h2>Command Substitution</h2>
|
||||
<pre><code>date=$(date) # Capture command output (modern syntax)
|
||||
date=`date` # Backticks (older syntax, less preferred)
|
||||
files=$(ls *.txt) # List of files
|
||||
count=$(wc -l < file.txt) # Line count
|
||||
|
||||
# Process substitution
|
||||
diff <(sort file1.txt) <(sort file2.txt)</code></pre>
|
||||
<hr>
|
||||
<h2>File Operations</h2>
|
||||
<pre><code># File tests
|
||||
[ -f "file.txt" ] # File exists and is regular file
|
||||
[ -d "dir" ] # Directory exists
|
||||
[ -r "file.txt" ] # File is readable
|
||||
[ -w "file.txt" ] # File is writable
|
||||
[ -x "file.txt" ] # File is executable
|
||||
[ -s "file.txt" ] # File exists and is not empty
|
||||
|
||||
# Reading files
|
||||
while IFS= read -r line; do
|
||||
echo "$line"
|
||||
done < file.txt
|
||||
|
||||
# Writing files
|
||||
echo "content" > file.txt # Overwrite
|
||||
echo "content" >> file.txt # Append</code></pre>
|
||||
<hr>
|
||||
<h2>Arithmetic</h2>
|
||||
<pre><code>x=10
|
||||
y=5
|
||||
|
||||
# Arithmetic expansion
|
||||
result=$((x + y)) # Addition
|
||||
result=$((x - y)) # Subtraction
|
||||
result=$((x * y)) # Multiplication
|
||||
result=$((x / y)) # Division
|
||||
result=$((x % y)) # Modulo
|
||||
result=$((x ** 2)) # Exponentiation
|
||||
|
||||
# Increment/decrement
|
||||
((x++)) # Post-increment
|
||||
((++x)) # Pre-increment
|
||||
((x--)) # Decrement</code></pre>
|
||||
<hr>
|
||||
<h2>Error Handling</h2>
|
||||
<pre><code>set -e # Exit on error
|
||||
set -u # Exit on undefined variable
|
||||
set -o pipefail # Exit on pipe failure
|
||||
|
||||
# Trap errors
|
||||
trap 'echo "Error occurred"' ERR
|
||||
|
||||
# Check command success
|
||||
if command; then
|
||||
echo "Success"
|
||||
else
|
||||
echo "Failed"
|
||||
fi
|
||||
|
||||
# Check exit code
|
||||
command
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "Success"
|
||||
fi</code></pre>
|
||||
<hr>
|
||||
<h2>Tips</h2>
|
||||
<ul>
|
||||
<li>Always quote variables to prevent word splitting: <code>"$var"</code> not <code>$var</code>.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Use <code>[[ ]]</code> instead of <code>[ ]</code> for more features and fewer surprises (Bash 4+).</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Use <code>local</code> in functions to avoid polluting global scope.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Test file existence with <code>-f</code> (file) or <code>-d</code> (directory) before operations.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Use <code>$(( ))</code> for arithmetic; it's safer than <code>expr</code>.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Prefer <code>$(command)</code> over backticks for command substitution.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Use <code>read -r</code> to prevent backslash interpretation when reading lines.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Set <code>IFS=</code> when reading to preserve leading/trailing whitespace.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Use <code>set -e</code> at the start of scripts to exit on errors (unless you handle them explicitly).</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Always start scripts with <code>#!/bin/bash</code> (or appropriate shebang) for portability.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Use <code>${var:-default}</code> to provide default values: <code>name=${1:-"World"}</code>.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Check if a command exists before using it:
|
||||
<pre><code>if command -v git &> /dev/null; then
|
||||
git --version
|
||||
fi</code></pre>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script async type="text/javascript" src="../blog/analytics.js"></script>
|
||||
<script src="../theme.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -0,0 +1,396 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; font-src 'self' data:; img-src 'self' data:; connect-src 'self'; base-uri 'self'; form-action 'self' https://defcon.social https://bsky.app;">
|
||||
<meta http-equiv="X-Content-Type-Options" content="nosniff">
|
||||
<link rel="stylesheet" href="../assets/css/style.css">
|
||||
<link rel="icon" type="image/x-icon" href="../favicon.ico">
|
||||
<script>
|
||||
// Apply theme immediately to prevent flash
|
||||
(function() {
|
||||
const theme = localStorage.getItem('theme') ||
|
||||
(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
|
||||
document.documentElement.setAttribute('data-theme', theme);
|
||||
})();
|
||||
</script>
|
||||
<title>C++ Quickstart Reference - Quick Reference - Launch Pad</title>
|
||||
</head>
|
||||
<body>
|
||||
<button class="theme-toggle" id="themeToggle" aria-label="Toggle dark mode">
|
||||
<svg class="theme-icon theme-icon-moon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path></svg>
|
||||
<svg class="theme-icon theme-icon-sun" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display: none;"><circle cx="12" cy="12" r="5"></circle><line x1="12" y1="1" x2="12" y2="3"></line><line x1="12" y1="21" x2="12" y2="23"></line><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line><line x1="1" y1="12" x2="3" y2="12"></line><line x1="21" y1="12" x2="23" y2="12"></line><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line></svg>
|
||||
</button>
|
||||
<br/><br/>
|
||||
<div class="name">
|
||||
__ _______________________ _________._________________________
|
||||
\_ _____/ \______ \ / _ \ / _____/ / _____/ | | \_ _____/
|
||||
| __) | _/ / /_\ \ / \ ___ / \ ___ | | | __)_
|
||||
| \ | | \ / | \ \ \_\ \ \ \_\ \ | |___ | \
|
||||
\___ / |____|_ / \____|__ / \______ / \______ / |_______ \ /_______ /
|
||||
\/ \/ \/ \/ \/ \/ \/
|
||||
</div>
|
||||
<div class="blog-page-header">
|
||||
<div class="blog-header-content">
|
||||
<a href="/quickref" class="back-link" title="Back to Quick Reference">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="42" height="42" viewBox="0 0 24 24" class="home-icon"><path fill="currentColor" d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/></svg>
|
||||
</a>
|
||||
<h1 class="blog-page-title">C++ Quickstart Reference</h1>
|
||||
</div>
|
||||
</div>
|
||||
<div class="blog-post-container">
|
||||
<div class="blog-posts-container" style="max-width: 900px; margin: 0 auto;">
|
||||
<div class="blog-post">
|
||||
<div class="blog-post-content">
|
||||
<p><a href="index.html">← Back to quick reference</a></p>
|
||||
<p><a href="../index.html">← Home</a></p>
|
||||
<hr>
|
||||
<p>Quick reference guide for C++. Essential syntax, memory management, STL containers, and common patterns for systems programming.</p>
|
||||
<hr>
|
||||
<h2>Hello World</h2>
|
||||
<pre><code>#include <iostream>
|
||||
|
||||
int main() {
|
||||
std::cout << "Hello, World!" << std::endl; // Standard output
|
||||
return 0;
|
||||
}</code></pre>
|
||||
<hr>
|
||||
<h2>Variables</h2>
|
||||
<pre><code>int x = 42; // integer
|
||||
double pi = 3.14; // floating point
|
||||
std::string name = "Alice"; // string class
|
||||
bool flag = true; // boolean
|
||||
char c = 'A'; // character
|
||||
|
||||
// Type modifiers
|
||||
unsigned int u = 100; // Unsigned
|
||||
long long big = 1000000; // Long integer
|
||||
float f = 3.14f; // Single precision
|
||||
|
||||
// Auto type deduction (C++11)
|
||||
auto y = 42; // Compiler infers int
|
||||
auto text = "Hello"; // Compiler infers const char*
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Control Flow</h2>
|
||||
<h3>If/Else</h3>
|
||||
<pre><code>if (x > 10) {
|
||||
std::cout << "x is big\n";
|
||||
} else {
|
||||
std::cout << "x is small\n";
|
||||
}
|
||||
|
||||
if (x > 10) {
|
||||
std::cout << "big\n";
|
||||
} else if (x > 5) {
|
||||
std::cout << "medium\n";
|
||||
} else {
|
||||
std::cout << "small\n";
|
||||
}
|
||||
|
||||
// Ternary operator
|
||||
int result = (x > 10) ? 100 : 0;
|
||||
</code></pre>
|
||||
<h3>Switch</h3>
|
||||
<pre><code>switch (x) {
|
||||
case 10:
|
||||
std::cout << "ten\n";
|
||||
break;
|
||||
case 20:
|
||||
std::cout << "twenty\n";
|
||||
break;
|
||||
default:
|
||||
std::cout << "other\n";
|
||||
}
|
||||
</code></pre>
|
||||
<h3>For Loops</h3>
|
||||
<pre><code>// Traditional for loop
|
||||
for (int i = 0; i < 5; i++) {
|
||||
std::cout << i << "\n";
|
||||
}
|
||||
|
||||
// Range-based for loop (C++11)
|
||||
int nums[] = {1, 2, 3};
|
||||
for (int n : nums) {
|
||||
std::cout << n << "\n";
|
||||
}
|
||||
|
||||
std::vector<int> vec = {4, 5, 6};
|
||||
for (int n : vec) {
|
||||
std::cout << n << "\n";
|
||||
}
|
||||
|
||||
// With auto
|
||||
for (auto n : vec) {
|
||||
std::cout << n << "\n";
|
||||
}
|
||||
</code></pre>
|
||||
<h3>While Loops</h3>
|
||||
<pre><code>while (x > 0) {
|
||||
x--;
|
||||
std::cout << x << "\n";
|
||||
}
|
||||
|
||||
// Do...while
|
||||
do {
|
||||
x--;
|
||||
} while (x > 0);
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Functions</h2>
|
||||
<pre><code>int add(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
std::cout << add(2, 3) << "\n";
|
||||
|
||||
// Function overloading
|
||||
int add(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
double add(double a, double b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
// Default parameters
|
||||
void greet(std::string name = "World") {
|
||||
std::cout << "Hello, " << name << "\n";
|
||||
}
|
||||
|
||||
// Pass by reference
|
||||
void increment(int& x) {
|
||||
x++;
|
||||
}
|
||||
|
||||
// Pass by pointer
|
||||
void setValue(int* ptr, int value) {
|
||||
*ptr = value;
|
||||
}
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Arrays / Vectors</h2>
|
||||
<h3>Arrays</h3>
|
||||
<pre><code>int nums[] = {1, 2, 3}; // Fixed-size array
|
||||
int arr[5] = {1, 2, 3, 4, 5}; // Explicit size
|
||||
|
||||
for (int n : nums) {
|
||||
std::cout << n << "\n";
|
||||
}
|
||||
|
||||
// Array size
|
||||
int size = sizeof(nums) / sizeof(nums[0]);
|
||||
</code></pre>
|
||||
<h3>Vectors</h3>
|
||||
<pre><code>#include <vector>
|
||||
|
||||
std::vector<int> vec = {4, 5, 6}; // Dynamic array
|
||||
vec.push_back(7); // Add to end
|
||||
vec.pop_back(); // Remove from end
|
||||
vec.size(); // Size
|
||||
vec[0]; // Access by index
|
||||
vec.at(0); // Safe access (throws if out of bounds)
|
||||
|
||||
// Iterate
|
||||
for (auto it = vec.begin(); it != vec.end(); ++it) {
|
||||
std::cout << *it << "\n";
|
||||
}
|
||||
|
||||
// Range-based for
|
||||
for (int n : vec) {
|
||||
std::cout << n << "\n";
|
||||
}
|
||||
</code></pre>
|
||||
<h3>Other STL Containers</h3>
|
||||
<pre><code>#include <string>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <unordered_map>
|
||||
|
||||
// String
|
||||
std::string str = "Hello";
|
||||
str.length();
|
||||
str += " World";
|
||||
str.substr(0, 5);
|
||||
|
||||
// Map
|
||||
std::map<std::string, int> person;
|
||||
person["age"] = 30;
|
||||
person["name"]; // Access (creates if doesn't exist)
|
||||
|
||||
// Set
|
||||
std::set<int> numbers = {1, 2, 3};
|
||||
numbers.insert(4);
|
||||
numbers.find(2); // Returns iterator
|
||||
|
||||
// Unordered map (hash map)
|
||||
std::unordered_map<std::string, int> hash_map;
|
||||
hash_map["key"] = 42;
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Input</h2>
|
||||
<pre><code>std::string input_name;
|
||||
std::cout << "Enter your name: ";
|
||||
std::cin >> input_name; // Whitespace-delimited
|
||||
std::cout << "Hello " << input_name << "\n";
|
||||
|
||||
// Full line input
|
||||
std::string full_line;
|
||||
std::cout << "Enter a line: ";
|
||||
std::getline(std::cin, full_line); // Reads entire line
|
||||
std::cout << "You entered: " << full_line << "\n";
|
||||
|
||||
// Number input
|
||||
int number;
|
||||
std::cout << "Enter a number: ";
|
||||
std::cin >> number;
|
||||
std::cout << "You entered: " << number << "\n";
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Pointers & References</h2>
|
||||
<pre><code>int x = 42;
|
||||
int* ptr = &x; // Pointer to x
|
||||
int& ref = x; // Reference to x
|
||||
|
||||
*ptr = 100; // Dereference pointer
|
||||
ref = 200; // Modify through reference
|
||||
|
||||
// Null pointer
|
||||
int* null_ptr = nullptr; // C++11 (prefer over NULL)
|
||||
|
||||
// Dynamic allocation (avoid if possible)
|
||||
int* dynamic = new int(42);
|
||||
delete dynamic; // Must delete what you new
|
||||
|
||||
// Smart pointers (preferred)
|
||||
#include <memory>
|
||||
std::unique_ptr<int> smart = std::make_unique<int>(42);
|
||||
// Automatically deleted when out of scope
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Classes</h2>
|
||||
<pre><code>class Person {
|
||||
private:
|
||||
std::string name;
|
||||
int age;
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Person(std::string n, int a) : name(n), age(a) {}
|
||||
|
||||
// Getter
|
||||
std::string getName() const {
|
||||
return name;
|
||||
}
|
||||
|
||||
// Setter
|
||||
void setAge(int a) {
|
||||
age = a;
|
||||
}
|
||||
|
||||
// Method
|
||||
void greet() const {
|
||||
std::cout << "Hello, I'm " << name << "\n";
|
||||
}
|
||||
};
|
||||
|
||||
Person person("Alice", 30);
|
||||
person.greet();
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>File Operations</h2>
|
||||
<pre><code>#include <fstream>
|
||||
|
||||
// Reading
|
||||
std::ifstream file("file.txt");
|
||||
std::string line;
|
||||
while (std::getline(file, line)) {
|
||||
std::cout << line << "\n";
|
||||
}
|
||||
file.close();
|
||||
|
||||
// Writing
|
||||
std::ofstream out("file.txt");
|
||||
out << "Hello, World!\n";
|
||||
out.close();
|
||||
|
||||
// Check if file opened
|
||||
if (file.is_open()) {
|
||||
// File operations
|
||||
} else {
|
||||
std::cerr << "Failed to open file\n";
|
||||
}
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Error Handling</h2>
|
||||
<pre><code>#include <stdexcept>
|
||||
|
||||
// Throwing exceptions
|
||||
void divide(int a, int b) {
|
||||
if (b == 0) {
|
||||
throw std::runtime_error("Division by zero");
|
||||
}
|
||||
return a / b;
|
||||
}
|
||||
|
||||
// Catching exceptions
|
||||
try {
|
||||
int result = divide(10, 0);
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Error: " << e.what() << "\n";
|
||||
}
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Tips</h2>
|
||||
<ul>
|
||||
<li>Always prefer <strong>stack allocation</strong> (<code>int x;</code>) over <code>new</code>/<code>delete</code> unless necessary.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>If you must allocate dynamically, use <strong>smart pointers</strong> (<code>std::unique_ptr</code>, <code>std::shared_ptr</code>) to avoid leaks.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Remember to <code>#include</code> necessary headers (<code><iostream></code>, <code><vector></code>, <code><string></code>).</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Use RAII (Resource Acquisition Is Initialization) to manage resources safely.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li><code>std::cin</code> input is whitespace-delimited; use <code>std::getline()</code> for full lines.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Watch for buffer overflows with arrays; prefer <code>std::vector</code> or <code>std::string</code>.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>C++ variables are strongly typed; type conversions are explicit.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Use <code>const</code> for values that shouldn't change; <code>const</code> correctness is important.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Prefer range-based for loops (<code>for (auto x : container)</code>) when possible.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Use <code>auto</code> for type deduction when the type is obvious from context.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Initialize variables; uninitialized variables contain garbage values.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Use references (<code>&</code>) instead of pointers when you don't need null or reassignment.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Prefer standard library containers (<code>std::vector</code>, <code>std::string</code>) over C-style arrays.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Use <code>nullptr</code> instead of <code>NULL</code> or <code>0</code> for null pointers (C++11).</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script async type="text/javascript" src="../blog/analytics.js"></script>
|
||||
<script src="../theme.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; font-src 'self' data:; img-src 'self' data:; connect-src 'self'; base-uri 'self'; form-action 'self' https://duckduckgo.com;">
|
||||
<meta http-equiv="X-Content-Type-Options" content="nosniff">
|
||||
<link rel="stylesheet" href="../assets/css/style.css">
|
||||
<link rel="icon" type="image/x-icon" href="../favicon.ico">
|
||||
<script>
|
||||
// Apply theme immediately to prevent flash
|
||||
(function() {
|
||||
const theme = localStorage.getItem('theme') ||
|
||||
(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
|
||||
document.documentElement.setAttribute('data-theme', theme);
|
||||
})();
|
||||
</script>
|
||||
<title>Quick Reference - Launch Pad</title>
|
||||
</head>
|
||||
<body>
|
||||
<button class="theme-toggle" id="themeToggle" aria-label="Toggle dark mode">
|
||||
<svg class="theme-icon theme-icon-moon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path></svg>
|
||||
<svg class="theme-icon theme-icon-sun" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display: none;"><circle cx="12" cy="12" r="5"></circle><line x1="12" y1="1" x2="12" y2="3"></line><line x1="12" y1="21" x2="12" y2="23"></line><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line><line x1="1" y1="12" x2="3" y2="12"></line><line x1="21" y1="12" x2="23" y2="12"></line><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line></svg>
|
||||
</button>
|
||||
<br/><br/>
|
||||
<div class="name">
|
||||
__ _______________________ _________._________________________
|
||||
/ \ / \_ _____/\______ \/ _____/| \__ ___/\_ _____/
|
||||
\ \/\/ /| __)_ | | _/\_____ \ | | | | | __)_
|
||||
\ / | \ | | \/ \| | | | | \
|
||||
\__/\ / /_______ / |______ /_______ /|___| |____| /_______ /
|
||||
\/ \/ \/ \/ \/
|
||||
|
||||
</div>
|
||||
<div class="blog-page-header">
|
||||
<div class="blog-header-content">
|
||||
<a href="/" class="back-link" title="Back to Home">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="42" height="42" viewBox="0 0 24 24" class="home-icon"><path fill="currentColor" d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/></svg>
|
||||
</a>
|
||||
<h1 class="blog-page-title">Quick Reference</h1>
|
||||
</div>
|
||||
</div>
|
||||
<div class="blog-posts-container">
|
||||
<p style="text-align: center; margin-bottom: 30px; font-size: 16px; color: var(--text-secondary);">
|
||||
Quick reference guides for programming languages and frameworks.<br>
|
||||
Syntax, examples, and common patterns at a glance.
|
||||
</p>
|
||||
|
||||
<div style="max-width: 800px; margin: 0 auto;">
|
||||
<div class="blog-post-preview">
|
||||
<h2 class="blog-post-title"><a href="bash.html" class="blog-post-link">Bash</a></h2>
|
||||
<p>Quickstart reference for Bash. Essential syntax, control flow, variables, arrays, and common patterns for shell scripting.</p>
|
||||
</div>
|
||||
<div class="blog-post-preview">
|
||||
<h2 class="blog-post-title"><a href="python.html" class="blog-post-link">Python</a></h2>
|
||||
<p>Quickstart reference for Python. Syntax, control flow, data structures, and essential tips.</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script async type="text/javascript" src="../blog/js/analytics.js"></script>
|
||||
<script src="../assets/js/theme.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,414 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; font-src 'self' data:; img-src 'self' data:; connect-src 'self'; base-uri 'self'; form-action 'self' https://defcon.social https://bsky.app;">
|
||||
<meta http-equiv="X-Content-Type-Options" content="nosniff">
|
||||
<link rel="stylesheet" href="../assets/css/style.css">
|
||||
<link rel="icon" type="image/x-icon" href="../favicon.ico">
|
||||
<script>
|
||||
// Apply theme immediately to prevent flash
|
||||
(function() {
|
||||
const theme = localStorage.getItem('theme') ||
|
||||
(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
|
||||
document.documentElement.setAttribute('data-theme', theme);
|
||||
})();
|
||||
</script>
|
||||
<title>JavaScript Quickstart Reference - Quick Reference - Launch Pad</title>
|
||||
</head>
|
||||
<body>
|
||||
<button class="theme-toggle" id="themeToggle" aria-label="Toggle dark mode">
|
||||
<svg class="theme-icon theme-icon-moon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path></svg>
|
||||
<svg class="theme-icon theme-icon-sun" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display: none;"><circle cx="12" cy="12" r="5"></circle><line x1="12" y1="1" x2="12" y2="3"></line><line x1="12" y1="21" x2="12" y2="23"></line><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line><line x1="1" y1="12" x2="3" y2="12"></line><line x1="21" y1="12" x2="23" y2="12"></line><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line></svg>
|
||||
</button>
|
||||
<br/><br/>
|
||||
<div class="name">
|
||||
__ _______________________ _________._________________________
|
||||
\_ _____/ \______ \ / _ \ / _____/ / _____/ | | \_ _____/
|
||||
| __) | _/ / /_\ \ / \ ___ / \ ___ | | | __)_
|
||||
| \ | | \ / | \ \ \_\ \ \ \_\ \ | |___ | \
|
||||
\___ / |____|_ / \____|__ / \______ / \______ / |_______ \ /_______ /
|
||||
\/ \/ \/ \/ \/ \/ \/
|
||||
</div>
|
||||
<div class="blog-page-header">
|
||||
<div class="blog-header-content">
|
||||
<a href="/quickref" class="back-link" title="Back to Quick Reference">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="42" height="42" viewBox="0 0 24 24" class="home-icon"><path fill="currentColor" d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/></svg>
|
||||
</a>
|
||||
<h1 class="blog-page-title">JavaScript Quickstart Reference</h1>
|
||||
</div>
|
||||
</div>
|
||||
<div class="blog-post-container">
|
||||
<div class="blog-posts-container" style="max-width: 900px; margin: 0 auto;">
|
||||
<div class="blog-post">
|
||||
<div class="blog-post-content">
|
||||
<p><a href="index.html">← Back to quick reference</a></p>
|
||||
<p><a href="../index.html">← Home</a></p>
|
||||
<hr>
|
||||
<p>Quick reference guide for JavaScript. Essential syntax, control flow, data structures, and common patterns for web development and Node.js.</p>
|
||||
<hr>
|
||||
<h2>Hello World</h2>
|
||||
<pre><code>console.log("Hello, World!"); // Basic output</code></pre>
|
||||
<hr>
|
||||
<h2>Variables</h2>
|
||||
<pre><code>let x = 42; // Mutable variable
|
||||
const name = "Alice"; // Immutable
|
||||
let flag = true; // Boolean
|
||||
var old = "deprecated"; // Avoid var, use let/const
|
||||
|
||||
// Multiple declarations
|
||||
let a = 1, b = 2, c = 3;
|
||||
|
||||
// Destructuring
|
||||
let [x, y] = [1, 2];
|
||||
let {name, age} = {name: "Alice", age: 30};
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Control Flow</h2>
|
||||
<h3>If/Else</h3>
|
||||
<pre><code>if (x > 10) {
|
||||
console.log("x is big");
|
||||
} else {
|
||||
console.log("x is small");
|
||||
}
|
||||
|
||||
if (x > 10) {
|
||||
console.log("big");
|
||||
} else if (x > 5) {
|
||||
console.log("medium");
|
||||
} else {
|
||||
console.log("small");
|
||||
}
|
||||
|
||||
// Ternary operator
|
||||
let result = x > 10 ? "big" : "small";
|
||||
|
||||
// Switch
|
||||
switch (x) {
|
||||
case 10:
|
||||
console.log("ten");
|
||||
break;
|
||||
case 20:
|
||||
console.log("twenty");
|
||||
break;
|
||||
default:
|
||||
console.log("other");
|
||||
}
|
||||
</code></pre>
|
||||
<h3>For Loops</h3>
|
||||
<pre><code>// Traditional for loop
|
||||
for (let i = 0; i < 5; i++) {
|
||||
console.log(i);
|
||||
}
|
||||
|
||||
// For...of (arrays, strings)
|
||||
let nums = [1, 2, 3];
|
||||
for (let n of nums) {
|
||||
console.log(n);
|
||||
}
|
||||
|
||||
// For...in (object keys)
|
||||
let person = {name: "Alice", age: 30};
|
||||
for (let key in person) {
|
||||
console.log(key, person[key]);
|
||||
}
|
||||
|
||||
// Array methods
|
||||
nums.forEach(n => console.log(n));
|
||||
nums.forEach((n, i) => console.log(i, n));
|
||||
</code></pre>
|
||||
<h3>While Loops</h3>
|
||||
<pre><code>while (x > 0) {
|
||||
x--;
|
||||
console.log(x);
|
||||
}
|
||||
|
||||
// Do...while
|
||||
do {
|
||||
x--;
|
||||
} while (x > 0);
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Functions</h2>
|
||||
<pre><code>// Function declaration
|
||||
function add(a, b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
console.log(add(2, 3));
|
||||
|
||||
// Function expression
|
||||
const subtract = function(a, b) {
|
||||
return a - b;
|
||||
};
|
||||
|
||||
// Arrow functions
|
||||
const multiply = (a, b) => {
|
||||
return a * b;
|
||||
};
|
||||
|
||||
const divide = (a, b) => a / b; // Implicit return
|
||||
|
||||
// Default parameters
|
||||
function greet(name = "World") {
|
||||
return `Hello, ${name}!`;
|
||||
}
|
||||
|
||||
// Rest parameters
|
||||
function sum(...numbers) {
|
||||
return numbers.reduce((a, b) => a + b, 0);
|
||||
}
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Arrays / Objects</h2>
|
||||
<h3>Arrays</h3>
|
||||
<pre><code>let nums = [1, 2, 3];
|
||||
nums.forEach(n => console.log(n));
|
||||
|
||||
// Array methods
|
||||
nums.push(4); // Add to end
|
||||
nums.pop(); // Remove from end
|
||||
nums.unshift(0); // Add to beginning
|
||||
nums.shift(); // Remove from beginning
|
||||
nums.length // Length
|
||||
nums[0] // Access by index
|
||||
nums.slice(1, 3) // Slice (non-mutating)
|
||||
nums.splice(1, 1) // Remove/insert (mutating)
|
||||
|
||||
// Array methods (functional)
|
||||
nums.map(n => n * 2) // Transform
|
||||
nums.filter(n => n > 1) // Filter
|
||||
nums.reduce((a, b) => a + b, 0) // Reduce
|
||||
nums.find(n => n > 1) // Find first match
|
||||
nums.some(n => n > 5) // Any match
|
||||
nums.every(n => n > 0) // All match
|
||||
</code></pre>
|
||||
<h3>Objects</h3>
|
||||
<pre><code>let person = {name: "Alice", age: 30};
|
||||
console.log(person.name);
|
||||
console.log(person["name"]);
|
||||
|
||||
// Object methods
|
||||
person.greet = function() {
|
||||
return `Hello, I'm ${this.name}`;
|
||||
};
|
||||
|
||||
// Object literal with method shorthand
|
||||
let person = {
|
||||
name: "Alice",
|
||||
age: 30,
|
||||
greet() {
|
||||
return `Hello, I'm ${this.name}`;
|
||||
}
|
||||
};
|
||||
|
||||
// Object destructuring
|
||||
let {name, age} = person;
|
||||
let {name: n, age: a} = person; // Rename
|
||||
|
||||
// Spread operator
|
||||
let person2 = {...person, age: 31};
|
||||
</code></pre>
|
||||
<h3>Maps and Sets</h3>
|
||||
<pre><code>// Map
|
||||
let map = new Map();
|
||||
map.set("name", "Alice");
|
||||
map.set("age", 30);
|
||||
map.get("name");
|
||||
map.has("name");
|
||||
map.delete("name");
|
||||
|
||||
// Set
|
||||
let set = new Set([1, 2, 3]);
|
||||
set.add(4);
|
||||
set.has(3);
|
||||
set.delete(2);
|
||||
set.size
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Strings</h2>
|
||||
<pre><code>let text = "Hello, World!";
|
||||
let template = `Hello, ${name}!`; // Template literal
|
||||
|
||||
// String methods
|
||||
text.length
|
||||
text.toUpperCase()
|
||||
text.toLowerCase()
|
||||
text.substring(0, 5)
|
||||
text.slice(0, 5)
|
||||
text.split(",")
|
||||
text.replace("World", "JavaScript")
|
||||
text.includes("Hello")
|
||||
text.startsWith("Hello")
|
||||
text.endsWith("!")
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Classes</h2>
|
||||
<pre><code>class Person {
|
||||
constructor(name, age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
greet() {
|
||||
return `Hello, I'm ${this.name}`;
|
||||
}
|
||||
}
|
||||
|
||||
let person = new Person("Alice", 30);
|
||||
console.log(person.greet());
|
||||
|
||||
// Inheritance
|
||||
class Student extends Person {
|
||||
constructor(name, age, grade) {
|
||||
super(name, age);
|
||||
this.grade = grade;
|
||||
}
|
||||
}
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Promises & Async/Await</h2>
|
||||
<pre><code>// Promises
|
||||
let promise = new Promise((resolve, reject) => {
|
||||
setTimeout(() => resolve("Done!"), 1000);
|
||||
});
|
||||
|
||||
promise.then(result => console.log(result))
|
||||
.catch(error => console.error(error));
|
||||
|
||||
// Async/await
|
||||
async function fetchData() {
|
||||
try {
|
||||
let response = await fetch("https://api.example.com/data");
|
||||
let data = await response.json();
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error("Error:", error);
|
||||
}
|
||||
}
|
||||
|
||||
// Promise.all
|
||||
let promises = [promise1, promise2, promise3];
|
||||
Promise.all(promises).then(results => {
|
||||
console.log(results);
|
||||
});
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Error Handling</h2>
|
||||
<pre><code>try {
|
||||
let result = riskyOperation();
|
||||
} catch (error) {
|
||||
console.error("Error:", error.message);
|
||||
} finally {
|
||||
console.log("Always runs");
|
||||
}
|
||||
|
||||
// Throwing errors
|
||||
function divide(a, b) {
|
||||
if (b === 0) {
|
||||
throw new Error("Division by zero");
|
||||
}
|
||||
return a / b;
|
||||
}
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Modules (ES6)</h2>
|
||||
<pre><code>// Export
|
||||
export function add(a, b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
export const PI = 3.14159;
|
||||
|
||||
export default class Calculator {
|
||||
// ...
|
||||
}
|
||||
|
||||
// Import
|
||||
import { add, PI } from "./math.js";
|
||||
import Calculator from "./calculator.js";
|
||||
import * as math from "./math.js";
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Node.js Basics</h2>
|
||||
<pre><code>// File system
|
||||
const fs = require("fs");
|
||||
|
||||
// Reading
|
||||
fs.readFile("file.txt", "utf8", (err, data) => {
|
||||
if (err) throw err;
|
||||
console.log(data);
|
||||
});
|
||||
|
||||
// Writing
|
||||
fs.writeFile("file.txt", "Hello, World!", (err) => {
|
||||
if (err) throw err;
|
||||
});
|
||||
|
||||
// Promises (fs/promises)
|
||||
const fs = require("fs/promises");
|
||||
let data = await fs.readFile("file.txt", "utf8");
|
||||
|
||||
// Readline (user input)
|
||||
const readline = require("readline");
|
||||
const rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
});
|
||||
|
||||
rl.question("What's your name? ", (answer) => {
|
||||
console.log(`Hello, ${answer}!`);
|
||||
rl.close();
|
||||
});
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Tips</h2>
|
||||
<ul>
|
||||
<li>Variables: <code>let</code> mutable, <code>const</code> immutable. Avoid <code>var</code>.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li><code>prompt()</code> (browser) or <code>readline</code> (Node.js) can handle user input.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>JS is dynamically typed; type coercion can surprise you. Use <code>===</code> for strict equality.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Arrays are zero-indexed; objects are key-value maps.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Functions are first-class and can be anonymous or arrow-style.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Use <code>const</code> by default, <code>let</code> when reassignment is needed.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Template literals (backticks) allow string interpolation: <code>`Hello, ${name}!`</code>.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Arrow functions preserve <code>this</code> from enclosing scope; regular functions have their own <code>this</code>.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Use array methods (<code>map</code>, <code>filter</code>, <code>reduce</code>) for functional programming patterns.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Destructuring works with arrays and objects for cleaner code.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Use <code>async/await</code> for cleaner asynchronous code than promise chains.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li><code>null</code> is intentional absence of value; <code>undefined</code> is uninitialized.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Use <code>===</code> and <code>!==</code> instead of <code>==</code> and <code>!=</code> to avoid type coercion surprises.</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script async type="text/javascript" src="../blog/analytics.js"></script>
|
||||
<script src="../theme.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -0,0 +1,415 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; font-src 'self' data:; img-src 'self' data:; connect-src 'self'; base-uri 'self'; form-action 'self' https://defcon.social https://bsky.app;">
|
||||
<meta http-equiv="X-Content-Type-Options" content="nosniff">
|
||||
<link rel="stylesheet" href="../assets/css/style.css">
|
||||
<link rel="icon" type="image/x-icon" href="../favicon.ico">
|
||||
<script>
|
||||
// Apply theme immediately to prevent flash
|
||||
(function() {
|
||||
const theme = localStorage.getItem('theme') ||
|
||||
(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
|
||||
document.documentElement.setAttribute('data-theme', theme);
|
||||
})();
|
||||
</script>
|
||||
<title>Odin Quickstart Reference - Quick Reference - Launch Pad</title>
|
||||
</head>
|
||||
<body>
|
||||
<button class="theme-toggle" id="themeToggle" aria-label="Toggle dark mode">
|
||||
<svg class="theme-icon theme-icon-moon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path></svg>
|
||||
<svg class="theme-icon theme-icon-sun" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display: none;"><circle cx="12" cy="12" r="5"></circle><line x1="12" y1="1" x2="12" y2="3"></line><line x1="12" y1="21" x2="12" y2="23"></line><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line><line x1="1" y1="12" x2="3" y2="12"></line><line x1="21" y1="12" x2="23" y2="12"></line><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line></svg>
|
||||
</button>
|
||||
<br/><br/>
|
||||
<div class="name">
|
||||
__ _______________________ _________._________________________
|
||||
\_ _____/ \______ \ / _ \ / _____/ / _____/ | | \_ _____/
|
||||
| __) | _/ / /_\ \ / \ ___ / \ ___ | | | __)_
|
||||
| \ | | \ / | \ \ \_\ \ \ \_\ \ | |___ | \
|
||||
\___ / |____|_ / \____|__ / \______ / \______ / |_______ \ /_______ /
|
||||
\/ \/ \/ \/ \/ \/ \/
|
||||
</div>
|
||||
<div class="blog-page-header">
|
||||
<div class="blog-header-content">
|
||||
<a href="/quickref" class="back-link" title="Back to Quick Reference">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="42" height="42" viewBox="0 0 24 24" class="home-icon"><path fill="currentColor" d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/></svg>
|
||||
</a>
|
||||
<h1 class="blog-page-title">Odin Quickstart Reference</h1>
|
||||
</div>
|
||||
</div>
|
||||
<div class="blog-post-container">
|
||||
<div class="blog-posts-container" style="max-width: 900px; margin: 0 auto;">
|
||||
<div class="blog-post">
|
||||
<div class="blog-post-content">
|
||||
<p><a href="index.html">← Back to quick reference</a></p>
|
||||
<p><a href="../index.html">← Home</a></p>
|
||||
<hr>
|
||||
<p>Quick reference guide for Odin. Essential syntax, types, control flow, and common patterns for the Odin programming language.</p>
|
||||
<hr>
|
||||
<h2>Hello World</h2>
|
||||
<pre><code>package main
|
||||
|
||||
import "core:fmt"
|
||||
|
||||
main :: proc() {
|
||||
fmt.println("Hello, World!") // Odin keeps it clean and readable.
|
||||
}</code></pre>
|
||||
<hr>
|
||||
<h2>Variables</h2>
|
||||
<pre><code>x: int = 42
|
||||
name: string = "Alice"
|
||||
flag: bool = true
|
||||
pi: f64 = 3.14
|
||||
|
||||
// Type inference with :=
|
||||
y := 100 // Compiler infers int
|
||||
text := "Hello" // Compiler infers string
|
||||
|
||||
// Multiple declarations
|
||||
a, b, c: int = 1, 2, 3
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Control Flow</h2>
|
||||
<h3>If/Else</h3>
|
||||
<pre><code>if x > 10 {
|
||||
fmt.println("x is big")
|
||||
} else {
|
||||
fmt.println("x is small")
|
||||
}
|
||||
|
||||
if x > 10 {
|
||||
fmt.println("big")
|
||||
} else if x > 5 {
|
||||
fmt.println("medium")
|
||||
} else {
|
||||
fmt.println("small")
|
||||
}
|
||||
|
||||
// If as expression
|
||||
result := "big" if x > 10 else "small"
|
||||
</code></pre>
|
||||
<h3>For Loops</h3>
|
||||
<pre><code>for i in 0..5 { // 0 to 4, exclusive
|
||||
fmt.println(i)
|
||||
}
|
||||
|
||||
for i in 0..=5 { // 0 to 5, inclusive
|
||||
fmt.println(i)
|
||||
}
|
||||
|
||||
nums := []int{1, 2, 3}
|
||||
for n in nums {
|
||||
fmt.println(n)
|
||||
}
|
||||
|
||||
for n, i in nums { // Value and index
|
||||
fmt.printf("%d: %d\n", i, n)
|
||||
}
|
||||
|
||||
// Traditional C-style
|
||||
for i := 0; i < 5; i += 1 {
|
||||
fmt.println(i)
|
||||
}
|
||||
</code></pre>
|
||||
<h3>While Loops</h3>
|
||||
<pre><code>y := 10
|
||||
for y > 0 {
|
||||
y -= 1
|
||||
fmt.println(y)
|
||||
}
|
||||
|
||||
// Infinite loop
|
||||
for {
|
||||
if y <= 0 {
|
||||
break
|
||||
}
|
||||
y -= 1
|
||||
}
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Functions</h2>
|
||||
<pre><code>add := fn(a: int, b: int) -> int {
|
||||
return a + b
|
||||
}
|
||||
|
||||
fmt.println(add(2, 3))
|
||||
|
||||
// Named function
|
||||
add :: proc(a: int, b: int) -> int {
|
||||
return a + b
|
||||
}
|
||||
|
||||
// Multiple return values
|
||||
divide :: proc(a: f64, b: f64) -> (f64, bool) {
|
||||
if b == 0 {
|
||||
return 0, false
|
||||
}
|
||||
return a / b, true
|
||||
}
|
||||
|
||||
result, ok := divide(10.0, 2.0)
|
||||
if ok {
|
||||
fmt.println(result)
|
||||
}
|
||||
|
||||
// Procedures (no return value)
|
||||
greet :: proc(name: string) {
|
||||
fmt.printf("Hello, %s!\n", name)
|
||||
}
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Arrays / Slices</h2>
|
||||
<h3>Slices</h3>
|
||||
<pre><code>nums: []int = [1, 2, 3] // Slice literal, mutable and resizable
|
||||
for n in nums {
|
||||
fmt.println(n)
|
||||
}
|
||||
|
||||
// Append to slice
|
||||
append(&nums, 4)
|
||||
append(&nums, 5, 6)
|
||||
|
||||
// Length and capacity
|
||||
len(nums) // Current length
|
||||
cap(nums) // Capacity
|
||||
|
||||
// Make slice with capacity
|
||||
nums := make([]int, 0, 10) // Length 0, capacity 10
|
||||
</code></pre>
|
||||
<h3>Arrays</h3>
|
||||
<pre><code>arr: [3]int = {1, 2, 3} // Fixed-size array
|
||||
arr := [3]int{1, 2, 3} // Shorthand
|
||||
|
||||
// Array size inference
|
||||
arr := [?]int{1, 2, 3} // Compiler infers size 3
|
||||
</code></pre>
|
||||
<h3>Maps</h3>
|
||||
<pre><code>import "core:map"
|
||||
|
||||
person := make(map[string]string)
|
||||
map.set(&person, "name", "Alice")
|
||||
map.set(&person, "age", "30")
|
||||
|
||||
name, found := map.get(&person, "name")
|
||||
if found {
|
||||
fmt.println(name)
|
||||
}
|
||||
|
||||
// Map literal
|
||||
person := map[string]string{
|
||||
"name" = "Alice",
|
||||
"age" = "30",
|
||||
}
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Strings</h2>
|
||||
<pre><code>text := "Hello, World!"
|
||||
text := `Multiline
|
||||
string
|
||||
literal`
|
||||
|
||||
// String operations
|
||||
len(text) // Length in bytes
|
||||
fmt.printf("%s\n", text) // Print formatted
|
||||
|
||||
// String concatenation
|
||||
greeting := "Hello, " + name
|
||||
|
||||
// String formatting
|
||||
name := "Alice"
|
||||
greeting := fmt.tprintf("Hello, %s!", name)
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Structs</h2>
|
||||
<pre><code>Person :: struct {
|
||||
name: string,
|
||||
age: int,
|
||||
}
|
||||
|
||||
person := Person{
|
||||
name = "Alice",
|
||||
age = 30,
|
||||
}
|
||||
|
||||
fmt.printf("%s is %d\n", person.name, person.age)
|
||||
|
||||
// Methods
|
||||
Person :: struct {
|
||||
name: string,
|
||||
age: int,
|
||||
}
|
||||
|
||||
greet :: proc(p: Person) {
|
||||
fmt.printf("Hello, I'm %s\n", p.name)
|
||||
}
|
||||
|
||||
person := Person{name = "Alice", age = 30}
|
||||
greet(person)
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Enums</h2>
|
||||
<pre><code>Direction :: enum {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right,
|
||||
}
|
||||
|
||||
dir := Direction.Up
|
||||
|
||||
switch dir {
|
||||
case .Up:
|
||||
fmt.println("Going up")
|
||||
case .Down:
|
||||
fmt.println("Going down")
|
||||
case .Left:
|
||||
fmt.println("Going left")
|
||||
case .Right:
|
||||
fmt.println("Going right")
|
||||
}
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Unions</h2>
|
||||
<pre><code>Value :: union {
|
||||
int,
|
||||
f64,
|
||||
string,
|
||||
}
|
||||
|
||||
v: Value = 42
|
||||
v = 3.14
|
||||
v = "hello"
|
||||
|
||||
switch v in v {
|
||||
case int:
|
||||
fmt.printf("Integer: %d\n", v)
|
||||
case f64:
|
||||
fmt.printf("Float: %f\n", v)
|
||||
case string:
|
||||
fmt.printf("String: %s\n", v)
|
||||
}
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Error Handling</h2>
|
||||
<pre><code>import "core:fmt"
|
||||
|
||||
// Using optional types
|
||||
divide :: proc(a: f64, b: f64) -> (result: f64, ok: bool) {
|
||||
if b == 0 {
|
||||
return 0, false
|
||||
}
|
||||
return a / b, true
|
||||
}
|
||||
|
||||
result, ok := divide(10.0, 2.0)
|
||||
if ok {
|
||||
fmt.printf("Result: %f\n", result)
|
||||
} else {
|
||||
fmt.println("Division by zero")
|
||||
}
|
||||
|
||||
// Using error types
|
||||
Error :: enum {
|
||||
None,
|
||||
Division_By_Zero,
|
||||
Invalid_Input,
|
||||
}
|
||||
|
||||
divide :: proc(a: f64, b: f64) -> (f64, Error) {
|
||||
if b == 0 {
|
||||
return 0, .Division_By_Zero
|
||||
}
|
||||
return a / b, .None
|
||||
}
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>File Operations</h2>
|
||||
<pre><code>import "core:os"
|
||||
import "core:fmt"
|
||||
|
||||
// Reading
|
||||
data, ok := os.read_entire_file("file.txt")
|
||||
if ok {
|
||||
fmt.println(string(data))
|
||||
}
|
||||
|
||||
// Writing
|
||||
content := "Hello, World!"
|
||||
os.write_entire_file("file.txt", transmute([]u8)content)
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Memory Management</h2>
|
||||
<pre><code>import "core:mem"
|
||||
|
||||
// Allocators
|
||||
allocator := context.allocator
|
||||
|
||||
// Allocate memory
|
||||
ptr := new(int)
|
||||
ptr^ = 42
|
||||
|
||||
// Free memory
|
||||
free(ptr)
|
||||
|
||||
// Using different allocators
|
||||
temp_allocator := mem.temp_allocator()
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Tips</h2>
|
||||
<ul>
|
||||
<li>Odin is strongly typed; type declarations are explicit.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Loops often use ranges like <code>0..5</code> (exclusive of 5); use <code>0..=5</code> for inclusive.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li><code>fmt.scanf</code> or <code>read_string</code> can handle user input:
|
||||
<pre><code>import "core:fmt"
|
||||
import "core:os"
|
||||
|
||||
input: [256]u8
|
||||
n, _ := os.read(os.stdin, input[:])
|
||||
text := string(input[:n])</code></pre>
|
||||
</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Slices (<code>[]Type</code>) are dynamic arrays, arrays (<code>[N]Type</code>) are fixed-size.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Functions are first-class and can be assigned to variables.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Use <code>:=</code> for type inference, <code>:</code> for explicit typing.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Procedures (<code>proc</code>) are functions; use <code>fn</code> for function values.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Multiple return values are common; use tuples or named returns.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Use <code>^</code> to dereference pointers, <code>&</code> to take addresses.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Odin uses explicit memory management; understand allocators for advanced usage.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Struct literals use <code>=</code> for field assignment: <code>Person{name = "Alice"}</code>.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Use <code>switch</code> for pattern matching on enums and unions.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Import packages with <code>import "package:name"</code>; core library uses <code>"core:"</code> prefix.</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script async type="text/javascript" src="../blog/analytics.js"></script>
|
||||
<script src="../theme.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -0,0 +1,239 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; font-src 'self' data:; img-src 'self' data:; connect-src 'self'; base-uri 'self'; form-action 'self' https://defcon.social https://bsky.app;">
|
||||
<meta http-equiv="X-Content-Type-Options" content="nosniff">
|
||||
<link rel="stylesheet" href="../assets/css/style.css">
|
||||
<link rel="icon" type="image/x-icon" href="../favicon.ico">
|
||||
<script>
|
||||
// Apply theme immediately to prevent flash
|
||||
(function() {
|
||||
const theme = localStorage.getItem('theme') ||
|
||||
(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
|
||||
document.documentElement.setAttribute('data-theme', theme);
|
||||
})();
|
||||
</script>
|
||||
<title>Python Quickstart Reference - Quick Reference - Launch Pad</title>
|
||||
</head>
|
||||
<body>
|
||||
<button class="theme-toggle" id="themeToggle" aria-label="Toggle dark mode">
|
||||
<svg class="theme-icon theme-icon-moon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path></svg>
|
||||
<svg class="theme-icon theme-icon-sun" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display: none;"><circle cx="12" cy="12" r="5"></circle><line x1="12" y1="1" x2="12" y2="3"></line><line x1="12" y1="21" x2="12" y2="23"></line><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line><line x1="1" y1="12" x2="3" y2="12"></line><line x1="21" y1="12" x2="23" y2="12"></line><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line></svg>
|
||||
</button>
|
||||
<br/><br/>
|
||||
<div class="name">
|
||||
__ _______________________ _________._________________________
|
||||
/ \ / \_ _____/\______ \/ _____/| \__ ___/\_ _____/
|
||||
\ \/\/ /| __)_ | | _/\_____ \ | | | | | __)_
|
||||
\ / | \ | | \/ \| | | | | \
|
||||
\__/\ / /_______ / |______ /_______ /|___| |____| /_______ /
|
||||
\/ \/ \/ \/ \/
|
||||
|
||||
</div>
|
||||
<div class="blog-page-header">
|
||||
<div class="blog-header-content">
|
||||
<a href="/quickref" class="back-link" title="Back to Quick Reference">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="42" height="42" viewBox="0 0 24 24" class="home-icon"><path fill="currentColor" d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/></svg>
|
||||
</a>
|
||||
<h1 class="blog-page-title">Python Quickstart Reference</h1>
|
||||
</div>
|
||||
</div>
|
||||
<div class="blog-post-container">
|
||||
<div class="blog-posts-container" style="max-width: 900px; margin: 0 auto;">
|
||||
<div class="blog-post">
|
||||
<div class="blog-post-content">
|
||||
<p><a href="index.html">← Back to quick reference</a></p>
|
||||
<p><a href="../index.html">← Home</a></p>
|
||||
<hr>
|
||||
<p>Quick reference guide for Python. Essential syntax, control flow, data structures, and common patterns.</p>
|
||||
<hr>
|
||||
<h2>Hello World</h2>
|
||||
<pre><code>print("Hello, World!") # The universal programmer's rite of passage.</code></pre>
|
||||
<hr>
|
||||
<h2>Variables</h2>
|
||||
<pre><code>x = 42 # integer
|
||||
name = "Alice" # string
|
||||
flag = True # boolean
|
||||
pi = 3.14 # float
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Control Flow</h2>
|
||||
<h3>If/Else</h3>
|
||||
<pre><code>if x > 10: # Colon is mandatory; indentation rules supreme
|
||||
print("x is big")
|
||||
else:
|
||||
print("x is small")
|
||||
|
||||
if x > 10:
|
||||
print("big")
|
||||
elif x > 5:
|
||||
print("medium")
|
||||
else:
|
||||
print("small")
|
||||
</code></pre>
|
||||
<h3>For Loops</h3>
|
||||
<pre><code>for i in range(5): # i goes from 0 to 4
|
||||
print(i)
|
||||
|
||||
for item in [1, 2, 3]: # Iterate over list
|
||||
print(item)
|
||||
|
||||
for i in range(0, 10, 2): # Start, stop, step
|
||||
print(i) # Prints 0, 2, 4, 6, 8
|
||||
</code></pre>
|
||||
<h3>While Loops</h3>
|
||||
<pre><code>while x > 0: # Beware infinite loops
|
||||
x -= 1
|
||||
print(x)
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Functions</h2>
|
||||
<pre><code>def add(a, b): # 'def' starts the function
|
||||
return a + b
|
||||
|
||||
print(add(2, 3)) # Call a function with parentheses
|
||||
|
||||
def greet(name="World"): # Default parameter
|
||||
return f"Hello, {name}!"
|
||||
|
||||
greet() # Uses default
|
||||
greet("Alice") # Overrides default
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Lists / Dictionaries</h2>
|
||||
<h3>Lists</h3>
|
||||
<pre><code>nums = [1, 2, 3] # Lists are ordered, mutable
|
||||
for n in nums:
|
||||
print(n)
|
||||
|
||||
nums.append(4) # Add to end
|
||||
nums.insert(0, 0) # Insert at index
|
||||
nums.pop() # Remove and return last item
|
||||
nums[0] # Access by index
|
||||
nums[-1] # Last item (negative indexing)
|
||||
nums[1:3] # Slice: items 1 to 2 (not 3)
|
||||
</code></pre>
|
||||
<h3>Dictionaries</h3>
|
||||
<pre><code>person = {"name": "Alice", "age": 30} # Key-value mapping
|
||||
print(person["name"]) # Access via keys
|
||||
person["city"] = "NYC" # Add new key-value
|
||||
person.get("name") # Safe access (returns None if missing)
|
||||
person.get("phone", "N/A") # With default value
|
||||
|
||||
for key, value in person.items(): # Iterate over key-value pairs
|
||||
print(f"{key}: {value}")
|
||||
</code></pre>
|
||||
<h3>Tuples</h3>
|
||||
<pre><code>point = (3, 4) # Immutable ordered collection
|
||||
x, y = point # Unpacking
|
||||
</code></pre>
|
||||
<h3>Sets</h3>
|
||||
<pre><code>unique = {1, 2, 3} # Unordered collection of unique items
|
||||
unique.add(4) # Add item
|
||||
unique.remove(2) # Remove item
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>String Operations</h2>
|
||||
<pre><code>text = "Hello, World!"
|
||||
text.upper() # "HELLO, WORLD!"
|
||||
text.lower() # "hello, world!"
|
||||
text.split(",") # ["Hello", " World!"]
|
||||
text.replace("World", "Python") # "Hello, Python!"
|
||||
f"Value: {x}" # f-string formatting
|
||||
"Value: {}".format(x) # .format() method
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>List Comprehensions</h2>
|
||||
<pre><code>squares = [x**2 for x in range(10)] # [0, 1, 4, 9, 16, ...]
|
||||
evens = [x for x in range(10) if x % 2 == 0] # [0, 2, 4, 6, 8]
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Error Handling</h2>
|
||||
<pre><code>try:
|
||||
result = 10 / 0
|
||||
except ZeroDivisionError:
|
||||
print("Can't divide by zero!")
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
finally:
|
||||
print("This always runs")
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>File Operations</h2>
|
||||
<pre><code># Reading
|
||||
with open("file.txt", "r") as f:
|
||||
content = f.read()
|
||||
lines = f.readlines()
|
||||
|
||||
# Writing
|
||||
with open("file.txt", "w") as f:
|
||||
f.write("Hello, World!")
|
||||
|
||||
# Appending
|
||||
with open("file.txt", "a") as f:
|
||||
f.write("New line\n")
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Classes</h2>
|
||||
<pre><code>class Person:
|
||||
def __init__(self, name, age):
|
||||
self.name = name
|
||||
self.age = age
|
||||
|
||||
def greet(self):
|
||||
return f"Hello, I'm {self.name}"
|
||||
|
||||
person = Person("Alice", 30)
|
||||
print(person.greet())
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Tips</h2>
|
||||
<ul>
|
||||
<li>Indentation *is* syntax — Python hates lazy spacing.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li><code>input()</code> always returns a string; convert to int/float if needed:
|
||||
<pre><code>age = int(input("How old are you? "))</code></pre>
|
||||
</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Python is dynamically typed: flexible, but watch out for sneaky type errors.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Everything is an object, even functions and classes.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li><code>range(5)</code> goes 0..4, not 5 — the Pythonic gotcha.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Use <code>with</code> statements for file operations — automatic cleanup.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>List comprehensions are faster and more Pythonic than loops for simple transformations.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Use <code>enumerate()</code> when you need both index and value:
|
||||
<pre><code>for i, item in enumerate(items):
|
||||
print(f"{i}: {item}")</code></pre>
|
||||
</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Use <code>zip()</code> to iterate over multiple lists simultaneously:
|
||||
<pre><code>for name, age in zip(names, ages):
|
||||
print(f"{name} is {age}")</code></pre>
|
||||
</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Import modules with <code>import module</code> or specific functions with <code>from module import function</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script async type="text/javascript" src="../blog/analytics.js"></script>
|
||||
<script src="../theme.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -0,0 +1,372 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; font-src 'self' data:; img-src 'self' data:; connect-src 'self'; base-uri 'self'; form-action 'self' https://defcon.social https://bsky.app;">
|
||||
<meta http-equiv="X-Content-Type-Options" content="nosniff">
|
||||
<link rel="stylesheet" href="../assets/css/style.css">
|
||||
<link rel="icon" type="image/x-icon" href="../favicon.ico">
|
||||
<script>
|
||||
// Apply theme immediately to prevent flash
|
||||
(function() {
|
||||
const theme = localStorage.getItem('theme') ||
|
||||
(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
|
||||
document.documentElement.setAttribute('data-theme', theme);
|
||||
})();
|
||||
</script>
|
||||
<title>Rust Quickstart Reference - Quick Reference - Launch Pad</title>
|
||||
</head>
|
||||
<body>
|
||||
<button class="theme-toggle" id="themeToggle" aria-label="Toggle dark mode">
|
||||
<svg class="theme-icon theme-icon-moon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path></svg>
|
||||
<svg class="theme-icon theme-icon-sun" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display: none;"><circle cx="12" cy="12" r="5"></circle><line x1="12" y1="1" x2="12" y2="3"></line><line x1="12" y1="21" x2="12" y2="23"></line><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line><line x1="1" y1="12" x2="3" y2="12"></line><line x1="21" y1="12" x2="23" y2="12"></line><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line></svg>
|
||||
</button>
|
||||
<br/><br/>
|
||||
<div class="name">
|
||||
__ _______________________ _________._________________________
|
||||
\_ _____/ \______ \ / _ \ / _____/ / _____/ | | \_ _____/
|
||||
| __) | _/ / /_\ \ / \ ___ / \ ___ | | | __)_
|
||||
| \ | | \ / | \ \ \_\ \ \ \_\ \ | |___ | \
|
||||
\___ / |____|_ / \____|__ / \______ / \______ / |_______ \ /_______ /
|
||||
\/ \/ \/ \/ \/ \/ \/
|
||||
</div>
|
||||
<div class="blog-page-header">
|
||||
<div class="blog-header-content">
|
||||
<a href="/quickref" class="back-link" title="Back to Quick Reference">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="42" height="42" viewBox="0 0 24 24" class="home-icon"><path fill="currentColor" d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/></svg>
|
||||
</a>
|
||||
<h1 class="blog-page-title">Rust Quickstart Reference</h1>
|
||||
</div>
|
||||
</div>
|
||||
<div class="blog-post-container">
|
||||
<div class="blog-posts-container" style="max-width: 900px; margin: 0 auto;">
|
||||
<div class="blog-post">
|
||||
<div class="blog-post-content">
|
||||
<p><a href="index.html">← Back to quick reference</a></p>
|
||||
<p><a href="../index.html">← Home</a></p>
|
||||
<hr>
|
||||
<p>Quick reference guide for Rust. Essential syntax, ownership, borrowing, and common patterns.</p>
|
||||
<hr>
|
||||
<h2>Hello World</h2>
|
||||
<pre><code>fn main() {
|
||||
println!("Hello, World!"); // Semicolons required; braces define scope
|
||||
}</code></pre>
|
||||
<hr>
|
||||
<h2>Variables</h2>
|
||||
<pre><code>let x: i32 = 42; // Immutable by default
|
||||
let mut y: i32 = 10; // Mutable
|
||||
let name: &str = "Alice";
|
||||
let flag: bool = true;
|
||||
let pi: f64 = 3.14; // Floating point
|
||||
|
||||
// Type inference
|
||||
let z = 100; // Compiler infers i32
|
||||
let text = "Hello"; // &str
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Control Flow</h2>
|
||||
<h3>If/Else</h3>
|
||||
<pre><code>if x > 10 {
|
||||
println!("x is big");
|
||||
} else {
|
||||
println!("x is small");
|
||||
}
|
||||
|
||||
if x > 10 {
|
||||
println!("big");
|
||||
} else if x > 5 {
|
||||
println!("medium");
|
||||
} else {
|
||||
println!("small");
|
||||
}
|
||||
|
||||
// If as expression
|
||||
let result = if x > 10 { "big" } else { "small" };
|
||||
</code></pre>
|
||||
<h3>For Loops</h3>
|
||||
<pre><code>for i in 0..5 { // 0 to 4 (exclusive)
|
||||
println!("{}", i);
|
||||
}
|
||||
|
||||
for i in 0..=5 { // 0 to 5 (inclusive)
|
||||
println!("{}", i);
|
||||
}
|
||||
|
||||
let nums = vec![1, 2, 3];
|
||||
for n in &nums { // Borrow reference
|
||||
println!("{}", n);
|
||||
}
|
||||
|
||||
for (i, n) in nums.iter().enumerate() {
|
||||
println!("{}: {}", i, n);
|
||||
}
|
||||
</code></pre>
|
||||
<h3>While Loops</h3>
|
||||
<pre><code>while y > 0 {
|
||||
y -= 1;
|
||||
println!("{}", y);
|
||||
}
|
||||
|
||||
// Loop with break
|
||||
loop {
|
||||
if y <= 0 {
|
||||
break;
|
||||
}
|
||||
y -= 1;
|
||||
}
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Functions</h2>
|
||||
<pre><code>fn add(a: i32, b: i32) -> i32 {
|
||||
a + b // Expression returns value (no semicolon)
|
||||
}
|
||||
|
||||
println!("{}", add(2, 3));
|
||||
|
||||
// Explicit return
|
||||
fn subtract(a: i32, b: i32) -> i32 {
|
||||
return a - b; // Semicolon required with return
|
||||
}
|
||||
|
||||
// Unit type (no return value)
|
||||
fn greet() {
|
||||
println!("Hello!");
|
||||
}
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Vectors / HashMaps</h2>
|
||||
<h3>Vectors</h3>
|
||||
<pre><code>let nums = vec![1, 2, 3]; // Dynamic array
|
||||
for n in &nums {
|
||||
println!("{}", n);
|
||||
}
|
||||
|
||||
let mut vec = Vec::new();
|
||||
vec.push(1);
|
||||
vec.push(2);
|
||||
vec.pop(); // Remove and return last item
|
||||
vec[0] // Access by index
|
||||
vec.len() // Length
|
||||
</code></pre>
|
||||
<h3>HashMaps</h3>
|
||||
<pre><code>use std::collections::HashMap;
|
||||
|
||||
let mut person = HashMap::new();
|
||||
person.insert("name", "Alice");
|
||||
person.insert("age", "30");
|
||||
println!("{}", person["name"]);
|
||||
|
||||
// Safe access
|
||||
match person.get("name") {
|
||||
Some(value) => println!("{}", value),
|
||||
None => println!("Not found"),
|
||||
}
|
||||
|
||||
// Or use unwrap_or
|
||||
let name = person.get("name").unwrap_or(&"Unknown");
|
||||
</code></pre>
|
||||
<h3>Arrays</h3>
|
||||
<pre><code>let arr = [1, 2, 3]; // Fixed-size array
|
||||
let arr: [i32; 3] = [1, 2, 3]; // Explicit type and size
|
||||
</code></pre>
|
||||
<h3>Tuples</h3>
|
||||
<pre><code>let point = (3, 4);
|
||||
let (x, y) = point; // Destructuring
|
||||
let x = point.0; // Access by index
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Strings</h2>
|
||||
<pre><code>let s1 = "Hello"; // &str (string slice)
|
||||
let s2 = String::from("Hello"); // String (owned)
|
||||
let s3 = s2.clone(); // Clone owned string
|
||||
|
||||
// String operations
|
||||
let mut s = String::from("Hello");
|
||||
s.push_str(" World");
|
||||
s.push('!');
|
||||
let len = s.len();
|
||||
let slice = &s[0..5]; // String slice
|
||||
|
||||
// Formatting
|
||||
let name = "Alice";
|
||||
let greeting = format!("Hello, {}!", name);
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Ownership & Borrowing</h2>
|
||||
<pre><code>// Ownership
|
||||
let s1 = String::from("hello");
|
||||
let s2 = s1; // s1 moved to s2, s1 no longer valid
|
||||
// println!("{}", s1); // Error! s1 no longer owns the value
|
||||
|
||||
// Borrowing (references)
|
||||
let s1 = String::from("hello");
|
||||
let len = calculate_length(&s1); // Borrow, don't move
|
||||
println!("{}", s1); // Still valid
|
||||
|
||||
fn calculate_length(s: &String) -> usize {
|
||||
s.len()
|
||||
}
|
||||
|
||||
// Mutable references
|
||||
let mut s = String::from("hello");
|
||||
change(&mut s);
|
||||
|
||||
fn change(some_string: &mut String) {
|
||||
some_string.push_str(", world");
|
||||
}
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Structs</h2>
|
||||
<pre><code>struct Person {
|
||||
name: String,
|
||||
age: u32,
|
||||
}
|
||||
|
||||
let person = Person {
|
||||
name: String::from("Alice"),
|
||||
age: 30,
|
||||
};
|
||||
|
||||
println!("{} is {}", person.name, person.age);
|
||||
|
||||
// Methods
|
||||
impl Person {
|
||||
fn new(name: String, age: u32) -> Person {
|
||||
Person { name, age }
|
||||
}
|
||||
|
||||
fn greet(&self) {
|
||||
println!("Hello, I'm {}", self.name);
|
||||
}
|
||||
}
|
||||
|
||||
let person = Person::new(String::from("Alice"), 30);
|
||||
person.greet();
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Enums</h2>
|
||||
<pre><code>enum Direction {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right,
|
||||
}
|
||||
|
||||
let dir = Direction::Up;
|
||||
|
||||
match dir {
|
||||
Direction::Up => println!("Going up"),
|
||||
Direction::Down => println!("Going down"),
|
||||
Direction::Left => println!("Going left"),
|
||||
Direction::Right => println!("Going right"),
|
||||
}
|
||||
|
||||
// Enums with data
|
||||
enum Option<T> {
|
||||
Some(T),
|
||||
None,
|
||||
}
|
||||
|
||||
let some_number = Some(5);
|
||||
let no_number: Option<i32> = None;
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Error Handling</h2>
|
||||
<pre><code>// Result type
|
||||
fn divide(a: f64, b: f64) -> Result<f64, String> {
|
||||
if b == 0.0 {
|
||||
Err(String::from("Division by zero"))
|
||||
} else {
|
||||
Ok(a / b)
|
||||
}
|
||||
}
|
||||
|
||||
match divide(10.0, 2.0) {
|
||||
Ok(result) => println!("Result: {}", result),
|
||||
Err(e) => println!("Error: {}", e),
|
||||
}
|
||||
|
||||
// Using unwrap (panics on error)
|
||||
let result = divide(10.0, 2.0).unwrap();
|
||||
|
||||
// Using expect (custom panic message)
|
||||
let result = divide(10.0, 2.0).expect("Division failed");
|
||||
|
||||
// Using ? operator (propagates error)
|
||||
fn calculate() -> Result<f64, String> {
|
||||
let x = divide(10.0, 2.0)?;
|
||||
Ok(x * 2.0)
|
||||
}
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>File Operations</h2>
|
||||
<pre><code>use std::fs;
|
||||
use std::io::{self, Write};
|
||||
|
||||
// Reading
|
||||
let content = fs::read_to_string("file.txt")
|
||||
.expect("Failed to read file");
|
||||
|
||||
// Writing
|
||||
fs::write("file.txt", "Hello, World!")
|
||||
.expect("Failed to write file");
|
||||
|
||||
// With error handling
|
||||
match fs::read_to_string("file.txt") {
|
||||
Ok(content) => println!("{}", content),
|
||||
Err(e) => println!("Error: {}", e),
|
||||
}
|
||||
</code></pre>
|
||||
<hr>
|
||||
<h2>Tips</h2>
|
||||
<ul>
|
||||
<li>Rust variables are immutable by default; use <code>mut</code> to allow changes.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li><code>read_line</code> handles user input; parsing is required for numbers:
|
||||
<pre><code>use std::io;
|
||||
let mut input = String::new();
|
||||
io::stdin().read_line(&mut input).expect("Failed to read");
|
||||
let number: i32 = input.trim().parse().expect("Not a number");</code></pre>
|
||||
</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Ownership and borrowing rules prevent data races — Rust's core safety guarantee.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li><code>0..5</code> ranges are exclusive of the end value; use <code>0..=5</code> for inclusive.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Everything has a type; Rust's compiler is strict but helpful with error messages.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Use <code>&</code> for borrowing (references) and <code>&mut</code> for mutable borrowing.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Expressions (no semicolon) return values; statements (with semicolon) return unit <code>()</code>.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Use <code>match</code> for pattern matching — exhaustive and powerful.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Use <code>Option<T></code> for values that might not exist, <code>Result<T, E></code> for operations that might fail.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>The <code>?</code> operator is syntactic sugar for propagating errors in functions that return <code>Result</code>.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Use <code>unwrap()</code> sparingly — prefer proper error handling with <code>match</code> or <code>?</code>.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Structs define data; <code>impl</code> blocks define methods and associated functions.</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script async type="text/javascript" src="../blog/analytics.js"></script>
|
||||
<script src="../theme.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user