-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgeneric_sized.rs
71 lines (53 loc) · 1.44 KB
/
generic_sized.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#![allow(unused)]
// Sized and ?Sized
// - Indicates that a type's size is known or not at compile time
// - Rust needs to know how much memory to allocate for any value
// - All values of a type must use the same amount of memory
// Dynamically sized types (DST)
// - Size is known only at runtime
// - Must always be behind a pointer
fn f<T: Sized>(x: T) {}
// This will not compile - T must be a pointer
// fn g<T: ?Sized>(x: T) {}
fn g<T: ?Sized>(x: &T) {}
fn main() {
// Sized
// - Trait where the type's size is known at compile time
// - Automatically implemented for everything where the size is known at compile time
// - Necessary for allocating values on the stack
// Examples
// Primitive types
let i: i32 = 1;
let x: f64 = 1.0;
let b: bool = true;
// Structs and enums with Sized fields
struct S {
i: i32,
j: i32,
};
let s = S { i: 1, j: 1 };
// Fixed size array
let arr: [i32; 4] = [0; 4];
f(i);
f(s);
f(arr);
f(&arr);
f("rust");
// ?Sized
// - Type may or may not be Sized
// - Used for working with dynamically sized types (DST)
// Examples
// str and slices
let s: &str = "hello";
let slice: &[i32] = &[1, 2, 3];
g(s);
g(slice);
// Trait objects
let v: Box<dyn A> = Box::new(1u32);
d(v);
let v: Box<dyn A> = Box::new(1u32);
g(&v);
}
trait A {}
impl A for u32 {}
fn d(x: Box<dyn A>) {}