pub struct Counter {
count: u32,
label: Option<String>,
}
trait Incrementable {
fn increment(&mut self);
fn reset(&mut self);
}
impl Incrementable for Counter {
fn increment(&mut self) {
self.count += 1;
}
fn reset(&mut self) {
self.count = 0;
}
}
impl Counter {
pub fn new(initial: u32) -> Self {
Counter {
count: initial,
label: None,
}
}
pub fn with_label(initial: u32, label: &str) -> Self {
Counter {
count: initial,
label: Some(label.to_string()),
}
}
pub fn get_count(&self) -> u32 {
self.count
}
pub fn add(&mut self, value: u32) {
self.count += value;
}
}
enum Color {
Red,
Green,
Blue,
Custom(u8, u8, u8),
}
union MaybeFloat {
as_float: f64,
as_bytes: [u8; 8],
}
type Callback = fn(u32) -> u32;
const MAX_SIZE: usize = 1024;
static mut COUNTER: u32 = 0;
fn add<T>(a: T, b: T) -> T
where
T: std::ops::Add<Output = T>,
{
a + b
}
fn longest<'a>(s1: &'a str, s2: &'a str) -> &'a str {
if s1.len() > s2.len() {
s1
} else {
s2
}
}
struct Container<T: Clone> {
value: T,
}
impl<T: Clone> Container<T> {
fn get(&self) -> T {
self.value.clone()
}
}
macro_rules! init_vec {
($($val:expr),*) => {
vec![$($val),*]
};
}
macro_rules! count_items {
($($item:expr),*) => {
vec![$(($item, 1)),*].len()
};
}
mod inner {
pub fn inner_function() -> String {
"Hello from inner".to_string()
}
}
pub use inner::inner_function;
fn process_value(value: u32) -> u32 {
println!("Processing: {}", value);
let numbers = init_vec![1, 2, 3, 4, 5];
unsafe {
COUNTER += 1;
}
value * 2
}
async fn fetch_data() -> Result<String, &'static str> {
Ok("data".to_string())
}
trait Repository {
type Item;
fn get(&self, id: u32) -> Option<Self::Item>;
}
struct UserRepository;
impl Repository for UserRepository {
type Item = String;
fn get(&self, id: u32) -> Option<Self::Item> {
Some(format!("User {}", id))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct Config {
name: String,
enabled: bool,
}
#[derive(Debug, Clone, Copy)]
struct Point {
x: f64,
y: f64,
}
fn main() {
let mut counter = Counter::with_label(10, "main");
counter.increment();
let count = counter.get_count();
println!("Count: {}", count);
let result = process_value(5);
let vec = init_vec![1, 2, 3];
let color = Color::Custom(255, 0, 128);
match color {
Color::Red => println!("Red"),
Color::Green => println!("Green"),
Color::Blue => println!("Blue"),
Color::Custom(r, g, b) => println!("Custom: RGB({}, {}, {})", r, g, b),
}
let sum = add(5, 10);
let container = Container { value: 42 };
let val = container.get();
let inner_msg = inner_function();
}
struct WidgetA {
id: u32,
}
impl WidgetA {
pub fn new() -> Self {
WidgetA { id: 1 }
}
}
struct WidgetB {
id: u32,
}
impl WidgetB {
pub fn new() -> Self {
WidgetB { id: 2 }
}
}
trait LogSink {
fn log(&self, msg: &str);
}
struct Logger {
prefix: String,
}
impl LogSink for Logger {
fn log(&self, msg: &str) {
println!("[{}] {}", self.prefix, msg);
}
}
impl Logger {
pub fn new() -> Self {
Logger {
prefix: "knot".to_string(),
}
}
}
fn exercise_qualified_calls() {
let _a = WidgetA::new();
let _logger = Logger::new();
}