All primitive types implement Copy?

Do all primitive types in Rust implement a trait Copy?

It would be interesting to know, since such knowledge is certainly part of a thorough study of a new programming language.

+7
source share
1 answer

We can use the compiler to prove that something implements Copy. Using the list of primitives from the Rust programming language :

fn is_copy<T: Copy>() {}

fn main() {
    is_copy::<bool>();
    is_copy::<char>();
    is_copy::<i8>();
    is_copy::<i16>();
    is_copy::<i32>();
    is_copy::<i64>();
    is_copy::<u8>();
    is_copy::<u16>();
    is_copy::<u32>();
    is_copy::<u64>();
    is_copy::<isize>();
    is_copy::<usize>();
    is_copy::<f32>();
    is_copy::<f64>();
    is_copy::<fn()>();
}

There are several other types that I would consider "primitive":

  • Fixed links ( &T)
  • Volatile Links ( &mut T)
  • Raw Pointers ( *const T/ *mut T)

Copy, Copy, Copy:

// OK
is_copy::<&String>();
is_copy::<*const String>();
is_copy::<*mut String>();
// Not OK
is_copy::<&mut i32>();

:

; . Copy, Copy:

// OK
is_copy::<[i32; 1]>();
is_copy::<(i32, i32)>();
// Not OK
is_copy::<[Vec<i32>; 1]>();
is_copy::<(Vec<i32>, Vec<i32>)>();

. ([T]) (str) . - , (&[T]/&str). . , .

// OK
is_copy::<&str>();
is_copy::<&[i32]>();
// Not OK
is_copy::<str>();
is_copy::<[i32]>();

, , , . ( , ).

+11

Source: https://habr.com/ru/post/1665441/


All Articles