Movatterモバイル変換


[0]ホーム

URL:


mut

Keywordmut 

Source
Expand description

A mutable variable, reference, or pointer.

mut can be used in several situations. The first is mutable variables,which can be used anywhere you can bind a value to a variable name. Someexamples:

// A mutable variable in the parameter list of a function.fnfoo(mutx: u8, y: u8) -> u8 {    x += y;    x}// Modifying a mutable variable.letmuta =5;a =6;assert_eq!(foo(3,4),7);assert_eq!(a,6);

The second is mutable references. They can be created frommut variablesand must be unique: no other variables can have a mutable reference, nor ashared reference.

// Taking a mutable reference.fnpush_two(v:&mutVec<u8>) {    v.push(2);}// A mutable reference cannot be taken to a non-mutable variable.letmutv =vec![0,1];// Passing a mutable reference.push_two(&mutv);assert_eq!(v,vec![0,1,2]);
letmutv =vec![0,1];letmut_ref_v =&mutv;letref_v =&v;mut_ref_v.push(2);

Mutable raw pointers work much like mutable references, with the addedpossibility of not pointing to a valid object. The syntax is*mut Type.

More information on mutable references and pointers can be found in theReference.


[8]ページ先頭

©2009-2026 Movatter.jp