Macbook pro running OSX 10.9.5
1.0.0-beta.2 (Mac installer)
$ rustc --version --verbose
rustc 1.0.0-beta.2 (e9080ec39 2015-04-16) (built 2015-04-16)
binary: rustc
commit-hash: e9080ec39d9a44eb0773d648b348575b19f655e5
commit-date: 2015-04-16
build-date: 2015-04-16
host: x86_64-apple-darwin
release: 1.0.0-beta.2
cargo buildCargo builds target/debug/hello_world
Cargo fails with the following error:
$ cargo build
failed to parse manifest at `/Users/karl/Projects/rust/hello/Cargo.toml`
Caused by:
either a [lib] or [[bin]] section must be present
If I put "1.0" in the version field of Cargo.toml, it fails with the following error:
$ cargo build
failed to parse manifest at `/Users/karl/Projects/rust/hello/Cargo.toml`
Caused by:
cannot parse '1.0' as a semver for the key `package.version`
Could you gist the contents of the Cargo.toml you wrote? It looks like the version key needs to be updated with an extra numeric component (e.g. the book's version is 0.0.1)
It's very simple so I'll post it here. The first error was caused by this Cargo.toml (copied from the book):
[package]
name = "hello_world"
version = "0.0.1"
authors = [ "somebody" ]
The second error (the parse error) was caused by this Cargo.toml:
[package]
name = "hello_world"
version = "1.0"
authors = [ "somebody" ]
Is that second Cargo.toml in the book? Otherwise this is a legitimate error that Cargo is saying.
I just copied from (http://doc.rust-lang.org/1.0.0-beta.2/book/hello-cargo.html), where it says:
Put this inside:
[package]
name = "hello_world"
version = "0.0.1"
authors = [ "Your name <[email protected]>" ]
Oh sorry the second one. No, for the second one I had just assumed that you could put any dotted number like 1.0 in the version field. It seems to only work if the first digit is 0.
What about "1.0.0"?
Ah so the version field is required to be a valid semver which requires three components (period separated). Other than that though it looks like everything is working as intended here, so closing, but thanks for the report!
OK cool, but what do I need to put in the original Cargo.toml file to make it work? The book doesn't say.
@steveklabnik's suggestion of using "1.0.0" as the version should do the trick!
Oh yes, that solves the version issue, but there's still the main issue that it fails with this:
Caused by:
either a [lib] or [[bin]] section must be present
So I'm guessing that rust has changed since this section of the book was written, but as a result, I don't know what to put in a lib or bin section.
Ah that just means you need to have either src/lib.rs or src/main.rs to build.
Oh I think I found the problem:
I hadn't created the src directory, and instead had everything at the top level:
$ tree
.
├── Cargo.toml
└── main.rs
0 directories, 2 files
If you do this, it gives a misleading error:
$ cargo build
failed to parse manifest at `/Users/karl/Projects/rust/hello/Cargo.toml`
Caused by:
either a [lib] or [[bin]] section must be present
But move main.rs into a src subdir, and everything works:
$ tree
.
├── Cargo.toml
└── src
└── main.rs
1 directory, 2 files
$ cargo build
Compiling hello v0.1.0 (file:///Users/karl/Projects/rust/hello)
Normally you have a src/lib.rs for your library.
If you don't, you need [[bin]] or [lib] otherwise cargo doesn't know what to build.
Please somebody fix this in the book and also show tree of directories how is should be.
Yes, it's important to be very explicit in an introductory book. I have 20 years professional experience covering more than 10 languages, and this stopped me dead in the water. Even worse: the error message cargo spits out doesn't even come close to explaining what's actually wrong. It feels like the bad old days of cryptic C compilers.
Ahh I forgot about one important moment
you should to set version to 0.1.0 in Cargo.toml
cuz if you will try to run this command cargo new hello_world --bin then you will notice in Cargo.toml the correct version and it will be 0.1.0 according with current version of _Rust_
Just create an empty src/lib.rs
Thanks for this issue and comments. I'm really looking forward to working through the book but this shut me down quickly. So I'm echoing @kstenerud on the need for explicit docs.
The book obviously has this part wrong, as it should explicitly say to put Cargo.html into the root directory. Please fix it. Thanks.
just for my knowledge
what if i don't want to name my file as main.rs
is there a flag/variable in Cargo.toml that i can set
in order to let cargo know that which file to build
and not just the default src/main.rs file
@ishankhare07 yeah you can have something like:
[[bin]]
name = "foo"
path = "src/some-random-path.rs"
@alexcrichton thanks, that worked
Most helpful comment
@ishankhare07 yeah you can have something like: