본문 바로가기
Rust

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

by 꾸압 2023. 1. 15.

 

<Struct Part>

[예제 코드_1]

#[derive(Debug)]
struct Movie {
    title: String,
}

fn main() {
    let movie = Movie { 
        title: String::from("Rust")
    };
    do_something(movie);
}

fn do_something(movie: Movie) {
    println!("Movie: {:?}!", movie);
}

 

[실행 결과]

Movie: Movie { title: "Rust" }!

 


 

[예제 코드_2]

#[derive(Debug)]
struct Movie {
    title: String,
}

fn main() {
    let movie = Movie { 
        title: String::from("Rust")
    };
    do_something(movie);
    println!("Movie: {:?}", movie);
}

fn do_something(movie: Movie) {
    println!("Movie: {:?}!", movie);
}

 

[실행 결과]

error[E0382]: borrow of moved value: `movie`
  --> src/main.rs:10:29

 


 

[예제 코드_3]

#[derive(Debug)]
struct Movie {
    title: String,
}

fn main() {
    let movie = Movie { 
        title: String::from("Rust")
    };
    do_something(&movie);
    println!("Movie: {:?}", movie);
}

fn do_something(movie: &Movie) {
    println!("Movie: {:?}!", movie);
}

 

[실행 결과]

Movie: Movie { title: "Rust" }!
Movie: Movie { title: "Rust" }

 


 

[예제 코드_4]

#[derive(Debug, Clone)]
struct Movie {
    title: String,
}

fn main() {
    let movie = Movie { 
        title: String::from("Rust")
    };
    do_something(movie.clone());
    println!("Movie: {:?}", movie);
}

fn do_something(movie: Movie) {
    println!("Movie: {:?}!", movie);
}

 

[실행 결과]

Movie: Movie { title: "Rust" }!
Movie: Movie { title: "Rust" }

 


 

[예제 코드_5]

#[derive(Debug)]
struct Movie {
    title: String,
}

fn main() {
    let movie = Movie { 
        title: String::from("Rust")
    };
    println!("Movie: {:?}", movie);
    do_something(movie);
}

fn do_something(movie: Movie) {
    println!("Movie: {:?}!", movie);
}

 

[실행 결과]

Movie: Movie { title: "Rust" }
Movie: Movie { title: "Rust" }!

 


 

<Struct with u8 field Part>

[예제 코드_1]

#[derive(Debug)]
struct Book {
    id: u8,
}

fn main() {
    let book = Book { id: 1 };
    do_something(book);
}

fn do_something(book: Book) {
    println!("Book {:?}!", book);
}

 

[실행 결과]

Book: Book { id: 1 }!

 


 

[예제 코드_2]

#[derive(Debug)]
struct Book {
    id: u8,
}

fn main() {
    let book = Book { id: 1 };
    do_something(book);
    println!("Book: {:?}", book);
}

fn do_something(book: Book) {
    println!("Book: {:?}!", book);
}

 

[실행 결과]

error[E0382]: borrow of moved value: `book`
 --> src/main.rs:8:28

 


 

[예제 코드_3]

#[derive(Debug, Copy)]
struct Book {
    id: u8,
}

fn main() {
    let book = Book { id: 1 };
    do_something(book);
    println!("Book: {:?}", book);
}

fn do_something(book: Book) {
    println!("Book: {:?}", book);
}

 

[실행 결과]

error[E0277]: the trait bound `Book: Clone` is not satisfied
 --> src/main.rs:1:17

 


 

[예제 코드_4]

#[derive(Debug, Copy, Clone)]
struct Book {
    id: u8,
}

fn main() {
    let book = Book { id: 1 };
    do_somthing(book);
    println!("Book: {:?}", book);
}

fn do_somthing(book: Book) {
    println!("Book: {:?}!", book);
}

 

[실행 결과]

Book: Book { id: 1 }!
Book: Book { id: 1 }

 


 

[예제 코드_5]

#[derive(Debug)]
struct Book {
    id: u8,
}

fn main() {
    let book = Book { id: 1 };
    do_something(&book);
    println!("Book: {:?}", book);
}

fn do_something(book: &Book) {
    println!("Book: {:?}!", book);
}

 

[실행 결과]

Book: Book { id: 1 }!
Book: Book { id: 1 }

 


 

<Struct with two String field Part>

[예제 코드_1]

#[derive(Debug)]
struct Person {
    name: String,
    alias: String,
}

fn main() {
    let person = Person { 
        name: "John".to_string(),
        alias: "Johan".to_string(),
    };
    print_alias(person.alias);
}

fn print_alias(alias: String) {
    println!("Person: {:?}!", alias);
}

 

[실행 결과]

Person: "Johan"!

 


 

[예제 코드_2]

#[derive(Debug)]
struct Person {
    name: String,
    alias: String,
}

fn main() {
    let person = Person { 
        name: "John".to_string(),
        alias: "Johan".to_string(),
    };
    print_alias(person.alias);
    println!("{:?}", person);
}

fn print_alias(alias: String) {
    println!("Person: {:?}!", alias);
}

 

[실행 결과]

error[E0382]: borrow of partially moved value: `person`
  --> src/main.rs:12:22

 


 

[예제 코드_3]

#[derive(Debug)]
struct Person {
    name: String,
    alias: String,
}

fn main() {
    let person = Person { 
        name: "John".to_string(),
        alias: "Johan".to_string(),
    };
    print_alias(person.alias);
    print_name(person.name);
}

fn print_alias(alias: String) {
    println!("Alias: {:?}!", alias);
}

fn print_name(name: String) {
    println!("Name: {:?}!", name);
}

 

[실행 결과]

Alias: "Johan"!
Name: "John"!

 


 

[예제 코드_4]

#[derive(Debug)]
struct Person {
    name: String,
    alias: String,
}

fn main() {
    let person = Person { 
        name: "John".to_string(),
        alias: "Johan".to_string(),
    };
    print_alias(&person.alias);
    print_name(&person.name);
}

fn print_alias(alias: &str) {
    println!("Alias: {:?}!", alias);
}

fn print_name(name: &str) {
    println!("Name: {:?}!", name);
}

 

[실행 결과]

Alias: "Johan"!
Name: "John"!

 


 

[예제 코드_5]

#[derive(Debug)]
struct Person {
    name: String,
    alias: String,
}

fn main() {
    let person = Person { 
        name: "John".to_string(),
        alias: "Johan".to_string(),
    };
    print_alias(person.alias.clone());
    print_name(person.name.clone());
    println!("{:?}", person);
}

fn print_alias(alias: String) {
    println!("Alias: {:?}!", alias);
}

fn print_name(name: String) {
    println!("Name: {:?}!", name);
}

 

[실행 결과]

Alias: "Johan"!
Name: "John"!
Person { name: "John", alias: "Johan" }

 


 

[예제 코드_6]

#[derive(Debug)]
struct Person {
    name: String,
    alias: String,
}

fn main() {
    let person = Person { 
        name: "John".to_string(),
        alias: "Johan".to_string(),
    };
    println!("{:?}", person);
    print_alias(person.alias);
    print_name(person.name);
}

fn print_alias(alias: String) {
    println!("Alias: {:?}!", alias);
}

fn print_name(name: String) {
    println!("Name: {:?}!", name);
}

 

[실행 결과]

Person { name: "John", alias: "Johan" }
Alias: "Johan"!
Name: "John"!

 


 

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

<참조 2>

 

 

댓글