References
C++๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ๋ฌ์คํธ๋ ์ฐธ์กฐํ์ ๊ฐ์ต๋๋ค:
Like C++, Rust has references:
fn main() { let mut x: i32 = 10; let ref_x: &mut i32 = &mut x; *ref_x = 20; println!("x: {x}"); }
C++๊ณผ์ ์ฐจ์ด์ :
- Cํฌ์ธํฐ์ ์ ์ฌํ๊ฒ
ref_x
์ ํ ๋นํ ๊ฒฝ์ฐ ์ฐธ์กฐ๋ฅผ ํด์ ํด์ผ ํฉ๋๋ค. - ๋ฌ์คํธ๋ ํน์ ํ ๊ฒฝ์ฐ(๋ฉ์๋ ํธ์ถ)์ ์๋์ผ๋ก ์ฐธ์กฐ ํด์ ๋ฅผ ํฉ๋๋ค.
mut
๋ก ์ ์ธ๋ ์ฐธ์กฐ๋ ์๋ช ์ ๋ฐ๋ผ ๋ค๋ฅธ ๊ฐ์ผ๋ก ํ ๋น๋ ์ ์์ต๋๋ค.
Some differences from C++:
- We must dereference
ref_x
when assigning to it, similar to C pointers,- Rust will auto-dereference in some cases, in particular when invoking methods (try
count_ones
).- References that are declared as
mut
can be bound to different values over their lifetime.
์ญ์ฃผ
- count_ones ๋ฉ์๋๋ฅผ ๋ง๋ค์ด์ ํ ์คํธํด๋ณด์ธ์