Movatterモバイル変換


[0]ホーム

URL:


vec

std

Macrovec 

1.0.0 ·Source
macro_rules! vec {    () => { ... };    ($elem:expr; $n:expr) => { ... };    ($($x:expr),+ $(,)?) => { ... };}
Expand description

Creates aVec containing the arguments.

vec! allowsVecs to be defined with the same syntax as array expressions.There are two forms of this macro:

  • Create aVec containing a given list of elements:
letv =vec![1,2,3];assert_eq!(v[0],1);assert_eq!(v[1],2);assert_eq!(v[2],3);
  • Create aVec from a given element and size:
letv =vec![1;3];assert_eq!(v, [1,1,1]);

Note that unlike array expressions this syntax supports all elementswhich implementClone and the number of elements doesn’t have to bea constant.

This will useclone to duplicate an expression, so one should be carefulusing this with types having a nonstandardClone implementation. Forexample,vec![Rc::new(1); 5] will create a vector of five referencesto the same boxed integer value, not five references pointing to independentlyboxed integers.

Also, note thatvec![expr; 0] is allowed, and produces an empty vector.This will still evaluateexpr, however, and immediately drop the resulting value, sobe mindful of side effects.


[8]ページ先頭

©2009-2026 Movatter.jp