< 返回版块

2019-05-05 22:55    责任编辑:jay

标签:Tyro,Novice,Learning

Data Implementation

对象大小

/* It prints:
4 4 */
fn main() {
    print!("{} ", std::mem::size_of::<i32>());
    print!("{} ", std::mem::size_of_val(&12));
}

use

/* It prints:
4 4 */
fn main() {
    use std::mem;
    print!("{} ", mem::size_of::<i32>());
    print!("{} ", mem::size_of_val(&12));
}
/* It prints:
4 4 */
fn main() {
    use std::mem::size_of;
    use std::mem::size_of_val;
    print!("{} ", size_of::<i32>());
    print!("{} ", size_of_val(&12));
}
/* It prints:
4 4 */
fn main() {
    use std::mem::*;
    print!("{} ", size_of::<i32>());
    print!("{} ", size_of_val(&12));
}

基本类型

/* It prints:
1 1 2 2 4 4 8 8 4 8 1 4*/
fn main() {
    use std::mem::*;
    print!("{} {} {} {} {} {} {} {} {} {} {} {}",
        size_of::<i8>(),
        size_of::<u8>(),
        size_of::<i16>(),
        size_of::<u16>(),
        size_of::<i32>(),
        size_of::<u32>(),
        size_of::<i64>(),
        size_of::<u64>(),
        size_of::<f32>(),
        size_of::<f64>(),
        size_of::<bool>(),
        size_of::<char>());
}
/* In a 32-bit system, it prints:
4 4 4 4*/
/* In a 64-bit system, it prints:
8 8 8 8*/
fn main() {
    use std::mem::*;
    print!("{} {} {} {}",
        size_of::<isize>(),
        size_of::<usize>(),
        size_of::<&i8>(),
        size_of::<&u32>());
}
/* In a 32-bit system, it prints:
[1]
[2, 0]
[3, 0, 0, 0]
[4, 5, 6, 0, 0, 0, 0, 0]
[65, 0, 0, 0]
[1]
[<4 integer numbers>]
*/
/* In a 64-bit system, it prints:
[1]
[2, 0]
[3, 0, 0, 0]
[4, 5, 6, 0, 0, 0, 0, 0]
[65, 0, 0, 0]
[1]
[<8 integer numbers>]
*/
fn main() {
    fn as_bytes<T>(o: &T) -> &[u8] {
        unsafe {
            std::slice::from_raw_parts(
                o as *const _ as *const u8,
                std::mem::size_of::<T>())
        }
    }
    println!("{:?}", as_bytes(&1i8));
    println!("{:?}", as_bytes(&2i16));
    println!("{:?}", as_bytes(&3i32));
    println!("{:?}", as_bytes(&(4i64 + 5 * 256 + 6 * 256 * 256)));
    println!("{:?}", as_bytes(&'A'));
    println!("{:?}", as_bytes(&true));
    println!("{:?}", as_bytes(&&1i8));
}

字节位

/* It prints:
<an integer number> <an integer number> <an integer number>*/
fn main() {
    let b1 = true;
    let b2 = true;
    let b3 = false;
    print!("{} {} {}",
        &b1 as *const bool as usize,
        &b2 as *const bool as usize,
        &b3 as *const bool as usize);
}

复合数据类型

/* In a 32-bit system, it prints:
160 16 1600 1 16 12*/
/* In a 64-bit system, it prints:
160 16 1600 1 16 24*/
fn main() {
    enum E1 { E1a, E1b };
    enum E2 { E2a, E2b(f64) };
    use std::mem::*;
    print!("{} {} {} {} {} {}",
        size_of_val(&[0i16; 80]),
        size_of_val(&(0i16, 0i64)),
        size_of_val(&[(0i16, 0i64); 100]),
        size_of_val(&E1::E1a),
        size_of_val(&E2::E2a),
        size_of_val(&vec![(0i16, 0i64); 100]));
}

Vector

/* It prints:
0 0
1 4
2 4
3 4
4 4
5 8
*/
fn main() {
    let mut v = vec![0; 0];
    println!("{} {}", v.len(), v.capacity());
    v.push(11);
    println!("{} {}", v.len(), v.capacity());
    v.push(22);
    println!("{} {}", v.len(), v.capacity());
    v.push(33);
    println!("{} {}", v.len(), v.capacity());
    v.push(44);
    println!("{} {}", v.len(), v.capacity());
    v.push(55);
    println!("{} {}", v.len(), v.capacity());
}
/* It prints:
33*/
fn main() {
    let arr = [11, 22, 33];
    let i: usize = 2;
    print!("{}", arr[i]);
}
/* It could print:
0 0 0
1 1 4
5 5 8
9 9 16
17 17 32
33 33 64
65 65 128
129 129 256
257 257 512
513 513 1024
*/
fn main() {
    let mut v = vec![0; 0];
    let mut prev_capacity = std::usize::MAX;
    for i in 0..1_000 {
        let cap = v.capacity();
        if cap != prev_capacity {
            println!("{} {} {}", i, v.len(), cap);
            prev_capacity = cap;
        }
        v.push(1);
    }
}

Allocating Memory

// It does nothing.
fn main() {
    static _A: u32 = 3;
    static _B: i32 = -1_000_000;
    static _C: f64 = 5.7e10;
    static _D: u8 = 200;
}
// It does nothing.
fn main() {
    let _a: u32 = 3;
    let _b: i32 = -1_000_000;
    let _c: f64 = 5.7e10;
    let _d: u8 = 200;
}
// It does nothing.
fn main() {
    fn f1(x1: i32) {
        let y1 = 2 + x1;
    }
    fn f2(x2: i32) {
        f1(x2 + 7);
    }
    let k = 20;
    f1(k + 4);
    f2(30);
}
/* It prints:
1 0
2 0
3 0
4 0
   and so on, until a stack overflow runtime error.
*/
fn main() {
    const SIZE: usize = 100_000;
    const N_ARRAY: usize = 1_000_000;
    fn create_array() -> [u8; SIZE] { [0u8; SIZE] }
    fn recursive_func(n: usize) {
        let a = create_array();
        println!("{} {}", N_ARRAY - n + 1, a[0]);
        if n > 1 { recursive_func(n - 1) }
    }
    recursive_func(N_ARRAY);
}
/* It prints:
1 0
2 0
3 0
4 0
   and so on, until a stack overflow runtime error.
*/
fn main() {
    const SIZE: usize = 100_000;
    const N_ARRAY: usize = 1_000_000;
    fn create_array() -> Box<[u8; SIZE]> { Box::new([0u8; SIZE]) }
    fn recursive_func(n: usize) {
        let a = create_array();
        println!("{} {}", N_ARRAY - n + 1, a[0]);
        if n > 1 { recursive_func(n - 1) }
    }
    recursive_func(N_ARRAY);
}
/* It prints:
3.4 [1, 2, 3] 3.4 true*/
fn main() {
    fn f(p: &f64) {
        let a = Box::new(*p);
        {
            let b = Box::new([1, 2, 3]);
            print!("{} {:?}", *a, *b);
        }
        let c = Box::new(true);
        print!(" {} {}", a, c);
    }
    f(&3.4);
}
/* It prints:
7 7; 7 9 9*/
fn main() {
    let a = 7;
    let a_box: Box<i32>;
    let mut a_ref: &i32 = &a;
    print!("{} {};", a, *a_ref);
    a_box = Box::new(a + 2);
    a_ref = &*a_box;
    print!(" {} {} {}", a, *a_ref, *a_box);
}
/* It prints:
7 7; 7 7 9; 7 7 7*/
fn main() {
    let a = 7;
    let mut a_box: Box<i32>;
    let a_ref: &i32 = &a;
    print!("{} {};", a, a_ref);
    a_box = Box::new(a + 2);
    print!(" {} {} {};", a, a_ref, a_box);
    a_box = Box::new(*a_ref);
    print!(" {} {} {}", a, a_ref, a_box);
}