2017-01-01 5 views

答えて

8

何かがCopyを実装しているかどうかを証明するためにコンパイラを使用できます。 the list of primitives from The Rust Programming Languageの使用:

fn a_function() {} 

fn is_copy<T>(_val: T) where T: Copy {} 

fn main() { 
    is_copy(true); // boolean 
    is_copy('c'); // char 
    is_copy(42i8); 
    is_copy(42i16); 
    is_copy(42i32); 
    is_copy(42i64); 
    is_copy(42u8); 
    is_copy(42u16); 
    is_copy(42u32); 
    is_copy(42u64); 
    is_copy(42isize); 
    is_copy(42usize); 
    is_copy(42f32); 
    is_copy(42f64); 
    is_copy("hello"); // string slices 
    is_copy(a_function); // function pointers 
} 

ここで説明されていない3つのタイプがあります。

  • はタプル
  • は、これらのタイプは、多くの種類が含まれている可能性があるためだ
  • 配列
  • スライス

。それらはジェネリックよりもパラメータ化されています。

// OK 
is_copy([42]); 
is_copy((42, 42)); 
is_copy(&[42][..]); 
// Not OK 
is_copy([Vec::new()]); 
is_copy((Vec::new(), Vec::new())); 
is_copy(&[Vec::new()][..]); 

そしていつものように、documentation for a trait lists everything that implements that trait:すべて含まれている値がCopyあるなら、彼らは唯一のCopyです。 (bugs in the documentationがある場合を除く)。

+1

https://doc.rust-lang.org/std/marker/に記載されているプリミティブタイプ(https://doc.rust-lang.org/std/primitive.i64.htmlなど)は表示されません。 trait.Copy.html、何か不足していますか? – ArtemGr

+3

@ArtemGr guh、私は本当にそれが今修正されたと思った。私はその問題にリンクしました。 – Shepmaster

関連する問題