Example of an automated script that does most of this: https://github.com/surpher/PactSwiftMockServer/blob/fb8148866bb05f49a0b1dcaae66c67bad1e7eca7/Support/build_rust_dependencies
curl https://sh.rustup.rs -sSf | sh
use anyhow::Result; | |
// The same function as before that might fail. | |
fn get_user_id(username: &str) -> Result<u32> { | |
if username.is_empty() { | |
anyhow::bail!("Username cannot be empty"); | |
} | |
Ok(42) | |
} |
use anyhow::Result; | |
// A function that returns a Result. | |
// We use 'anyhow::Result' as a shorthand for 'Result<T, anyhow::Error>'. | |
fn get_user_id(username: &str) -> Result<u32> { | |
// This is a simple, illustrative error case. | |
// We can use anyhow::bail! to create an immediate error. | |
if username.is_empty() { | |
// 'anyhow::bail!' is a macro that returns an error. | |
anyhow::bail!("Username cannot be empty"); |
// https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=f6b6cbb5fedbb3a745ee98fde30c8d06 | |
// A basic struct that holds an array of 16-bit unsigned integers. | |
struct Numbers([u16; 10]); | |
// Implement the IntoIterator trait for our Numbers struct. | |
impl<'a> IntoIterator for &'a Numbers { | |
// We want to iterate over references to u16 values. | |
type Item = &'a u16; | |
// The type of the iterator we're returning. | |
type IntoIter = std::slice::Iter<'a, u16>; |
use std::rc::Rc; | |
fn process_shared(data: Rc<String>, storage: &mut Vec<Rc<String>>) { | |
storage.push(data.clone()); // Clones Rc, not String | |
} | |
fn main() { | |
let shared = Rc::new("shared".to_string()); | |
let mut storage = Vec::new(); | |
while Rc::strong_count(&shared) < 10 { | |
process_shared(shared.clone(), &mut storage); | |
println!("Rc count: {}", Rc::strong_count(&shared)); |
#[allow(dead_code)] | |
#[derive(Debug)] | |
enum ConfigError { | |
MissingKey(String), | |
ParseError(std::num::ParseIntError), | |
} | |
impl From<std::num::ParseIntError> for ConfigError { | |
fn from(err: std::num::ParseIntError) -> Self { | |
ConfigError::ParseError(err) | |
} |
use tokio::io::{AsyncReadExt, AsyncWriteExt}; | |
use tokio::net::TcpListener; | |
#[tokio::main] | |
async fn main() -> Result<(), Box<dyn std::error::Error>> { | |
let listener = TcpListener::bind("127.0.0.1:8080").await?; | |
println!("Echo server listening on 127.0.0.1:8080"); | |
loop { | |
let (mut socket, addr) = listener.accept().await?; |
use std::collections::BTreeMap; | |
fn main() { | |
// Let's create a nested B-tree structure: | |
// A BTreeMap where keys are strings (e.g., "country") | |
// and values are another BTreeMap (e.g., "city" -> population). | |
let mut world_populations: BTreeMap<String, BTreeMap<String, u64>> = BTreeMap::new(); | |
// Add some data for "USA" | |
let mut usa_cities = BTreeMap::new(); | |
usa_cities.insert("New York".to_string(), 8_468_000); |
fn is_palindrome(n: u64) -> bool { | |
if n < 10 { | |
return true; // Single-digit numbers are palindromes | |
} | |
let s = n.to_string(); | |
s == s.chars().rev().collect::<String>() | |
} | |
fn generate_palindromes_up_to(limit: u64) -> Vec<u64> { | |
let mut palindromes = Vec::new(); |
use tokio::io::{AsyncReadExt, AsyncWriteExt}; | |
use tokio::net::TcpListener; | |
#[tokio::main] | |
async fn main() -> Result<(), Box<dyn std::error::Error>> { | |
let listener = TcpListener::bind("127.0.0.1:8080").await?; | |
println!("Echo server listening on 127.0.0.1:8080"); | |
loop { | |
let (mut socket, addr) = listener.accept().await?; |
Example of an automated script that does most of this: https://github.com/surpher/PactSwiftMockServer/blob/fb8148866bb05f49a0b1dcaae66c67bad1e7eca7/Support/build_rust_dependencies
curl https://sh.rustup.rs -sSf | sh