#![allow(unused_variables)]
struct Struct<T> (T);
// clear && cargo run --example generics_type_hello
fn main() {
let instance = Struct(0u8);
let instance = Struct(0u32);
let instance = Struct('0');
let instance = Struct(0.0);
let instance = Struct("0.0");
let instance = Struct(());
let instance = Struct([0]);
let instance = Struct(Struct(0.0f64));
}
#![allow(unused_variables)]
use std::fmt::Debug;
fn print<T: Debug>(x: T) {
println!("{:?}", x);
}
// clear && cargo run --example generics_fn_hello
fn main() {
print(0u8);
print(0u32);
print('0');
print(0.0);
print("0.0");
print(());
print([0]);
}
#![allow(unused_variables)]
struct Struct<T> (T);
impl<S> Struct<S> {
fn get(&self) -> &S {
&self.0
}
}
// clear && cargo run --example generics_impl_hello
fn main() {
let instance = Struct(0u8);
instance.get();
let instance = Struct(0u32);
instance.get();
let instance = Struct('0');
instance.get();
let instance = Struct("0");
instance.get();
}
#![allow(unused_variables)]
struct Struct<T> (T);
trait Trait<U> {
fn get(&self) -> &U;
}
impl<S> Trait<S> for Struct<S> {
fn get(&self) -> &S {
&self.0
}
}
// clear && cargo run --example generics_trait_hello
fn main() {
let instance = Struct(0u8);
instance.get();
let instance = Struct(0u32);
instance.get();
let instance = Struct('0');
instance.get();
let instance = Struct("0");
instance.get();
}
// Dynamically Sized Types (DSTs)
// https://doc.rust-lang.org/nomicon/exotic-sizes.html
struct MySuperSliceable<T: ?Sized> {
info: u32,
data: T
}
fn main() {
let sized: MySuperSliceable<[u8; 8]> = MySuperSliceable {
info: 17,
data: [0; 8],
};
let dynamic: &MySuperSliceable<[u8]> = &sized;
// prints: "17 [0, 0, 0, 0, 0, 0, 0, 0]"
println!("{} {:?}", dynamic.info, &dynamic.data);
}