Code Monkey home page Code Monkey logo

belajar-rust's People

Contributors

alvinisasi avatar r17x avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

belajar-rust's Issues

Tidak bisa menjalankan hasil dari sub bab Associated Functions

Halo, saya mencoba kode sub bab Associated Functions di bab Struct.

impl People {
    fn count_income(d: i32, i: i32) -> People {
        People { days: d, income: i }
    }
}

fn main() {
    let show = People::count_income(30, 3000);
    println!("{:#?}", show);
}

Saat saya menjalankan kode di atas tidak berhasil karena modul People tidak dideklarasikan. Berikut screenshotnya.

Screen Shot 2020-09-27 at 16 33 16

Saya mencoba mendeklarasikan struct People sebelum kode impl People seperti di bawah ini.

struct People;

impl People {
    fn count_income(d: i32, i: i32) -> People {
        People { days: d, income: i }
    }
}

fn main() {
    let show = People::count_income(30, 3000);
    println!("{:#?}", show);
}

Namun, saat saya jalankan tidak berhasil karena People tidak memiliki field days dan income. Berikut screenshotnya.

Screen Shot 2020-09-27 at 16 37 02

Kemudian, saya coba menambahkan field days dan income di struct People kode masih tidak bisa dijalankan karena masalah formatting.

struct People {
    days: i32,
    income: i32
}

impl People {
    fn count_income(d: i32, i: i32) -> People {
        People { days: d, income: i }
    }
}

fn main() {
    let show = People::count_income(30, 3000);
    println!("{:#?}", show);
}

Screen Shot 2020-09-27 at 16 41 07

Akhirnya, saya coba tambahkan #[derive(Debug)] di atas struct People barulah berjalan sesuai dengan ekspektasi.

#[derive(Debug)]
struct People {
    days: i32,
    income: i32
}

impl People {
    fn count_income(d: i32, i: i32) -> People {
        People { days: d, income: i }
    }
}

fn main() {
    let show = People::count_income(30, 3000);
    println!("{:#?}", show);
}

Screen Shot 2020-09-27 at 16 42 51

Saya penasaran mengapa saya harus mendefinisikan struct People lagi? Padahal di dalam impl People sudah mendefinisikan struct People secara tidak langsung.

Cc mas @adiatma, @ri7nz, @kevanantha

Terima kasih.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.