본문 바로가기
Rust

[Rust] Ownership(소유권) 기초 예제 : i32 (32일차)

by 꾸압 2023. 1. 14.

 

<i32 Part>

[예제 코드_1]

fn main() {
    let age: i32 = 25;
    do_something(age);
}

fn do_something(age: i32) {
    println!("Hello, {}", age);
}

 

[실행 결과]

Hello, 25!

 


 

[예제 코드_2]

fn main() {
    let age: i32 = 25;
    do_something(age);
    println!("{}", age);
}

fn do_something(age: i32) {
    println!("Hello, {}", age);
}

 

[실행 결과]

Hello, 25!
25

 

[예제 코드_3]

fn main() {
    let age: i32 = 25;
    do_something(&age);
    println!("{}", age);
}

fn do_something(age: &i32) {
    println!("Hello, {}", age);
}

 

[실행 결과]

Hello, 25!
25

 


 

[예제 코드_4]

fn main() {
    let age: i32 = 25;
    do_something(age.clone());
    println!("{}", age);
}

fn do_something(age: i32) {
    println!("Hello, {}!", age);
}

 

[실행 결과]

Hello, 25!
25

 


 

<참조 1> https://itnext.io/rust-ownership-50-code-examples-96203fcf79ea

<참조 2>

 

 

댓글