Skip to content
Snippets Groups Projects
Commit cfd6b987 authored by Per Lindgren's avatar Per Lindgren
Browse files

examples

parent 27b7cf3b
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,8 @@ struct P<T> {
y: T,
}
// We can implement functions for
// instances of our generic type
impl P<u32> {
fn double(&mut self) {
self.x += self.x;
......@@ -11,6 +13,7 @@ impl P<u32> {
}
}
// Another implementation
impl P<u64> {
fn double(&mut self) {
self.x += self.x;
......@@ -26,10 +29,19 @@ fn main() {
let mut p2 = P { x: 1u64, y: 2 };
p2.double();
println!("x {} y {}", p2.x, p2.y);
let mut p3: P<u8> = P { x: 1u8, y: 2 };
// p3.double(); // we don't have an implementation (yet)
println!("x {} y {}", p3.x, p3.y);
}
// The types of p1 and p2 is known at compile time
// (See that rust analyses derives the types)
// (See that rust analyzer derives the types.)
//
// The compiler will "specialize" the call
// to double (as cheap as an ordinary call)
//
// We have a zero-cost abstraction.
//
// (We cannot do double on p3, as we don't have any
// implementation of double for P<u8>.)
// a struct with generics
// a trait for double
use std::fmt::Debug;
#[derive(Debug)]
struct P<T> {
x: T,
y: T,
}
// Declare an "interface", for double.
trait Double {
fn double(&mut self);
}
// Implement Double for some types.
impl Double for P<u32> {
fn double(&mut self) {
self.x += self.x;
self.y += self.y;
}
}
impl Double for String {
fn double(&mut self) {
let s = self.clone();
self.push_str(&s);
}
}
fn main() {
let mut p1 = P { x: 1u32, y: 2 };
p1.double();
println!("{:?}", p1);
let mut s = "String, ".to_string();
s.double();
println!("{:?}", s);
}
// Types of p1 and s is known at compile time
// Even if we now use Traits the compiler can
// specialize the calls to double.
//
// We have a zero-cost abstraction.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment