Gaetan (cc'ing rust-dev)-

On 18/12/2013 09:48, Gaetan wrote:
but I wouldn't understand why the 'mod' keyword would stay
Because a Rust module is not the same thing as a crate.

Changes to the syntax for linking to an external crate is orthogonal to the concept of a module. So as long as we want to be able to define modules (which are not crates), we probably want a `mod` keyword.

----


Quick tutorial
--------------

The keyword `mod` (without extern) is used to define modules.

A module gathers together a set of names (either by defining each name as some item in the module, or by importing them from the outside via `use`). Modules can be nested within one another:

    mod a {
        pub mod b {
            pub fn f(x:i32) -> i32 { 3 * x * x }
        }
        pub mod c {
            pub fn f(x:f32) -> f32 { 3.14 * x * x }
        }
    }

    fn main() {
        use a::{b, c};
        println!("b::f(2) = {} c::f(2) = {}", b::f(2), c::f(2.0))
    }

One could write a whole slew of modules in a single file by nesting them. But since many developers would prefer to be able to break their modules into multiple files, the `mod` form also lets you define a inner module but put its body into another file. So here's another way to write the `mod a` from above:

    mod a {
pub mod b; // (moved definition of pub fn f:i32 -> i32 to file at "a/b.rs") pub mod c; // (moved definition of pub fn f:f32 -> f32 to file at "a/c.rs")
    }

Modules are for namespace and static-access control.

On the other hand, a crate is a unit of compilation, composed of one or more modules nested together in a hierarchical structure. The rust compiler works on a whole crate at a time; this allows it to perform code analyses and transformations that cross module boundaries (such as the enforcing the coherence rule for trait implementations).

----

I believe all of this is spelled out in the "Crates and the module system" section [1] of the tutorial.

(There is always a single *root module* for each crate; it is the root of the tree hierarchy of modules. This correspondence may be the reason for your confusion differentiating the two concepts.)

The use of `extern mod` as a way to import a crate is on its way out, as discussed in another message. But that has nothing to do with the Rust's concept of a module.

Cheers,
-Felix

[1] http://static.rust-lang.org/doc/master/tutorial.html#crates-and-the-module-system

On 18/12/2013 09:48, Gaetan wrote:
I am in favor of replacing the mod keyword by crate.
#[package_id = "whoami"];
#[package_type = "lib"];
...
use crate whoamiextern

but I wouldn't understand why the 'mod' keyword would stay


-----
Gaetan



2013/12/18 Liigo Zhuang <com.li...@gmail.com <mailto:com.li...@gmail.com>>

    `use crate foo; ` looks good to me. i always think it should be
    optional. rustc can deduce which crate will be used, from use mods
    lines, in most situations.


    2013/12/18 Brian Anderson <bander...@mozilla.com
    <mailto:bander...@mozilla.com>>

        We discussed this some in the meeting today:
        https://github.com/mozilla/rust/wiki/Meeting-weekly-2013-12-17


        On 12/16/2013 06:41 PM, Liigo Zhuang wrote:

        2013/12/16 Brian Anderson <bander...@mozilla.com
        <mailto:bander...@mozilla.com>>

            My feeling is that it is a crate, since that's the name
            we've historically used. There's already been agreement
            to remove "extern mod" in favor of "crate".


        IMO, "package" is used in several languages, maybe it's much
        familiar and friendly to rust newbies:

```
            package newpkg; // pkgid is "newpkg", compile to dynamic
            library (.so)

            package main; // pkgid "main" means compile to executable
            program (.exe)

            static package newpkg; // pkgid is "newpkg", compile to
            static library

            extern package newpkg; // or ...

            extern package newpkg =
            "http://github.com/liigo/rust-newpkg#newpkg:1.0";;

            ```

        But I'm OK if "crate" is used here.

        Liigo, 2013-12-17.





-- by *Liigo*, http://blog.csdn.net/liigo/
    Google+ https://plus.google.com/105597640837742873343/

    _______________________________________________
    Rust-dev mailing list
    Rust-dev@mozilla.org <mailto:Rust-dev@mozilla.org>
    https://mail.mozilla.org/listinfo/rust-dev




_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev


--
irc: pnkfelix on irc.mozilla.org
email: {fklock, pnkfelix}@mozilla.com

_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to