Rust Nickel Hello World tutorial throwing dependency error when run - rust

I am learning Rust, and saw a post on http://reddit.com/r/rust yesterday for Nickel. As a Node.js developer in my free time, I was interested in checking this out.
I downloaded the Rust 1.0.0-beta DMG from http://rust-lang.org.
I followed the Hello World tutorial precisely, and when I execute cargo run in my Terminal, I am receiving the following error:
Robs-MacBook-Pro:nickel-demo rob$ cargo run
Compiling nickel-demo v0.0.1 (file:///Users/rob/Workbench/nickel-demo)
src/main.rs:4:1: 4:21 error: an external crate named `nickel` has already been imported into this module [E0259]
src/main.rs:4 extern crate nickel;
^~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
Could not compile `nickel-demo`.
The Hello World main.rs file for the Nickel Demo looks like this:
extern crate nickel;
#[macro_use] extern crate nickel_macros;
extern crate nickel;
use nickel::Nickel;
fn main() {
let mut server = Nickel::new();
server.utilize(router! {
get "**" => |_req, _res| {
"Hello world!"
}
});
server.listen("127.0.0.1:6767");
}
As I was typing this code into my editor, I specifically thought it was weird that I was declaring extern crate nickel; twice in the file. After receiving the error I refactored the code to this:
extern crate nickel;
#[macro_use] extern crate nickel_macros;
use nickel::Nickel;
...
And I get this error:
Robs-MacBook-Pro:nickel-demo rob$ cargo run
Compiling nickel-demo v0.0.1 (file:///Users/rob/Workbench/nickel-demo)
Running `target/debug/nickel-demo`
Listening on http://127.0.0.1:6767
Ctrl-C to shutdown server
thread '<main>' panicked at 'arithmetic operation overflowed', /Users/rob/.cargo/registry/src/github.com-1ecc6299db9ec823/hyper-0.3.11/src/server/mod.rs:90
An unknown error occurred

The arithmetic operation overflowed bug seems to be an upstream issue. It also affects hyper which nickel depends on.
See https://github.com/seanmonstar/num_cpus/issues/2
As a temporary workaround use cargo build --release to prevent the checks altogether.

Related

Rust #![no_std]: unresolved external symbol _mainCRTStartup

I wanted to start a Rust no_std project, so I simply created a new cargo package and wrote in main.rs the following lines:
#![feature(lang_items, start)]
#![no_std]
extern crate libc;
#[start]
fn main(_argc: isize, _argv: *const *const u8) -> isize{
1
}
#[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> !{
loop{}
}
#[lang = "eh_personality"] extern fn eh_personality() {}
The Cargo.toml file looks like that:
[dependencies]
libc = "0.2.71"
[profile.dev]
panic = "abort"
[profile.release]
panic = "abort"
I ran cargo build and the linker printed:
LINK : error LNK2001: unresolved external symbol _mainCRTStartup
What could be the reason behind that error ?
Default build target on windows uses mscv toolchain, which includes a dynamically-linked libc. This ones is a C-runtime, which, in particularly, includes an undefined main symbol (which assumed to be defined by a programmer), therefore at linking stage the linker cannot find this missing symbol. You need explicitly specify that you don't need it via #![no_main] at the beginning of main.rs.
I also would suggest you reading A Freestanding Rust Binary which explain much more in details.

Why do I get "can't find crate" that is listed as a dependency in Cargo.toml when I compile with rustc?

My Cargo.toml includes this:
[dependencies]
chrono = "0.4"
And my code includes this:
extern crate chrono;
use chrono::{Duration, DateTime, Utc};
yet when I run my code, I get this error:
error[E0463]: can't find crate for `chrono`
--> src/lib.rs:1:1
|
1 | extern crate chrono;
| ^^^^^^^^^^^^^^^^^^^^ can't find crate
I am working on an Exercism exercise, so the way I am building/running the program is rustc src/lib.rs to test my solution. Is the problem because I'm not running rustc src/main.rs?
When you're directly running rustc, all that compiler knows is the command line arguments. It doesn't know anything about Cargo.toml, in particular, and so it doesn't know where to look for chrono library.
To use dependency management, you have to compile your project with Cargo - just use cargo build/cargo run/cargo test, and everything should be fine. See the Book for details.
If, however, you want (for some reason) use rustc directly, I'd advise to check first cargo anyway, by using cargo build --verbose. It will show all the commands that are invoked, allowing you to inspect the possible arguments to be defined manually.

Unable to run a simple example -- "use of unstable library feature 'rustc_private': ....."

I'm trying to use simplelog.rs in my Rust application. The hello world example
#[macro_use]
extern crate log;
extern crate simplelog;
// ..........
CombinedLogger::init(vec![
TermLogger::new(LogLevelFilter::Warn, simplelog::Config::default()).unwrap(),
WriteLogger::new(LogLevelFilter::Info, simplelog::Config::default(), File::create("log.log").unwrap())]).unwrap();
results into this:
error: use of unstable library feature 'rustc_private': this crate is being loaded from the sysroot, an unstable location; did you mean to load this crate from crates.io via `Cargo.toml` instead? (see issue #27812)
--> src/main.rs:9:1
|
9 | extern crate log;
| ^^^^^^^^^^^^^^^^^
error: aborting due to previous error
How to fix it?
From the discussion of #27812 it's still unclear what to do about it.
You're missing log = "version" entry in Cargo.toml [dependencies].
Because of that Cargo doesn't give Rust a log crate to load with extern crate log, and Rust keeps looking for it deeper, finding some internal one.

"entry point could not be located" when running program on Windows

I wrote a program to parse some filenames in Rust using the standard Regex crate. The program runs fine on Linux, but when I tried to compile and run it on Windows, I get some kind of DLL error. I don't really understand what is going on with this, but it's all I have to go on.
This is the compiler version that I'm using:
F:\Coding\rust-shutterstock-deduper\target (master)
λ rustc --version
rustc 1.0.0-nightly (3ef8ff1f8 2015-02-12 00:38:24 +0000)
This is the program that I'm trying to run:
#![feature(plugin)]
#![plugin(regex_macros)]
extern crate regex_macros;
extern crate regex;
fn main() {
let x = regex!(".*");
}
And my Cargo.toml file:
[package]
name = "my_package"
version = "0.0.1"
authors = ["Nate Mara <natemara#gmail.com>"]
[dependencies]
regex = "0.1.14"
regex_macros = "0.1.8"
Are there compiler flags that I should be passing in, or do I need to run this in a special way, or... what am I doing wrong here? I'm just running with cargo run
Add #[no_link] to your code:
#![plugin(regex_macros)]
#[no_link]
extern crate regex_macros;
Right now, plugins are crates, which means they get linked in. The regex_macros crate should tell you to add no_link, but this is a temporary workaround for a Rust issue. However, it looks like this is in the process of being fixed.

"expected crate directive" error about rust

Editor's note: This question was asked before Rust 1.0 was released and the syntax, error messages, and even how crates are bundled have changed since then.
i wrote a rust example program.
code is this:
use std;
import std::io;
fn main() {
io::println("hello world");
}
i try to compile it, but the rustc compile tell me some error message .
hello.rc:4:0: 4:2 error: expected crate directive
hello.rc:4 fn main() {
^~
my rustc version is
macmatoMacBook-Air:test kula$ rustc -v
rustc 0.1.1 (a0f0a70 2012-03-10 00:35:02 -0800)
host: x86_64-apple-darwin
and my os is mac osx 10.7
anyone know what happened? i think my rust code has no problem on it.
The compiler is trying to be smart and behaves differently for .rs (source) and .rc (crate) files.
Try again with a .rs file extension! :)

Resources