Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do all primitive types implement the Copy trait?

Tags:

rust

Do all primitive types in Rust implement the Copy trait?

It would be interesting to know, as surely such knowledge is part of a thorough learning of a new programming language.

like image 581
D. Ataro Avatar asked Jan 01 '17 04:01

D. Ataro


People also ask

What makes a type primitive?

Primitive type refers to a whole host of less complex variables and data types in different technologies and programming syntax systems. Some of these are defined by whether the variable needs substructures, or how simple the data type is to represent.

What is copy trait in Rust?

pub trait Copy: Clone { } Types whose values can be duplicated simply by copying bits.

Are references copy rust?

Yes, this is correct. In Rust terms, &T is Copy , which means that it can be copied bitwise without transferring ownership.

What is a primitive type in coding?

Primitive types are the most basic data types available within the Java language. There are 8: boolean , byte , char , short , int , long , float and double . These types serve as the building blocks of data manipulation in Java. Such types serve only one purpose — containing pure, simple values of a kind.


1 Answers

We can use the compiler to prove if 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 a few other types I'd consider "primitive":

  • Immutable references (&T)
  • Mutable references (&mut T)
  • Raw pointers (*const T / *mut T)

Immutable references always implement Copy, mutable references never implement Copy, and raw pointers always implement Copy:

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

There are a few other types from the book's list:

  • Tuples
  • Arrays

These types can contain many types; they are parameterized over a generic. They are only Copy if all the contained values are Copy:

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

Slices are doubly special. The slice type itself ([T]) and string slices (str) are unsized types. It's very rare to see them without an indirection of some kind, often a reference (&[T] / &str). The unsized values cannot exist by themselves. The version behind a reference behaves like references do.

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

And as always, the documentation for a trait lists everything that implements that trait. (Except when there are bugs in the documentation).

like image 182
Shepmaster Avatar answered Sep 19 '22 04:09

Shepmaster