"expected crate directive" error about rust - 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! :)

Related

Unable to use rust crates

I'm new to rust. I'm following a getting started tutorial that imports the crate random-number but when running the code I'm getting the error can't find crate for 'random_number'. What am I doing wrong?
~/Cargo.toml:
[package]
name = "test"
version = "0.0.1"
edition = "2021"
[dependencies]
random-number = "0.1.8"
~/src/main.rs:
extern crate random_number;
use random_number::random;
fn main() {
let num: i8 = random!(..);
println!("{}", num);
}
rustc is not meant to be used directly. It is the compiler that can compile a .rs file, but it doesn't have any dependency manager attached to it. So if you decide to use rustc directly, you need to manage your dependencies manually.
cargo is the official tool to compile Rust projects. It internally uses rustc, but additionally manages the project's dependencies that are specified in Cargo.toml.
cargo build --release && ./target/release/<project_name>
or the short form:
cargo run --release

Missing crate when using rustc but not using cargo

I'm using the epub crate on version 1.2.3 and my Cargo.toml is formatted as such
[package]
name = "cl-epub-reader"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
epub = "1.2.22"
and my main.rs is
use epub::doc::EpubDoc;
use epub::doc::NavPoint;
use std::env::args;
fn main() {
let args: Vec<String> = args().collect();
let doc = EpubDoc::new(&args[1]).unwrap();
// assert!(doc.is_ok());
println!("{:?}", doc.mdata("title"));
}
All it does is get the uses the path from the a user input then it gets the title metadata and printing it.
When I run cargo run it outputs an error about an error about going out of range, which is expected however when I run rustc src/main.rs the error is error[E0433]: failed to resolve: maybe a missing crate `epub`?
Does anyone know the cause of this and how to solve this issue?
When you call rustc directly, you need to pass the appropriate options to tell the compiler where the crates are installed on your computer. cargo handles that automatically for you (you can use the -v option to cargo build to see the actual rustc command lines it uses).

How to use Rust's async/await syntax in WASI

I would like to compile the following code with cargo-wasi.
// reqwest = { version = "0.11", features = ["json"] }
// tokio = { version = "1", features = ["full"] }
use std::collections::HashMap;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let resp = reqwest::get("https://httpbin.org/ip")
.await?
.json::<HashMap<String, String>>()
.await?;
println!("{:#?}", resp);
Ok(())
}
After trying to compile it, I got the following error because mio doesn't currently support WASI
$ cargo wasi run
Compiling mio v0.7.9
Compiling parking_lot v0.11.1
Compiling serde_json v1.0.64
Compiling idna v0.2.2
error[E0432]: unresolved import `crate::sys::IoSourceState`
--> /home/ducaale/.cargo/registry/src/github.com-1ecc6299db9ec823/mio-0.7.9/src/io_source.rs:12:5
|
12 | use crate::sys::IoSourceState;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ no `IoSourceState` in `sys`
... errors omitted
I did some research and none of the examples I found so far were utilizing async/await. Is there something I could replace tokio with so my code compiles in WASI?
I tried running this and it seems the reqwests crate doesn't build correctly with cargo wasi or wasm-pack because it's unable to compile mio (which is used by tokio when compiling natively). There is some mention on github that reqwests can be used with wasm, but it's not yet fully supported, and I couldn't find much on how to make it work. It sounds like there isn't much solution yet for HTTP requests on WASI at this time, but web-sys can be used to make requests through Node.js or a browser.
It appears that tokio requires specific features flags in order to be used with web assembly. This issue mentions the sync and rt flags at the bottom: https://github.com/tokio-rs/tokio/issues/1597 but in order to also use #[tokio:main], you will need the "rt-multi-thread" and "macros" feature flags as well.
It might also be possible to use wasm bindgen to convert the future into a promise, but that might not work with WASI: https://rustwasm.github.io/wasm-bindgen/api/wasm_bindgen_futures/
Try removing networking from tokio:
tokio = { version="1.21.1", features=["rt","sync","time"] }

cannot find function `get_platform` in sdl2 0.31.0

I'm using the latest version of sdl2 (0.31.0) but cannot access get_platform:
extern crate sdl2;
pub fn main() {
println!("{}", sdl2::get_platform());
}
$ cargo run
Compiling repro v0.1.0 (file:///private/tmp/repro)
error[E0425]: cannot find function `get_platform` in module `sdl2`
--> src/main.rs:4:30
|
4 | println!("{}", sdl2::get_platform());
| ^^^^^^^^^^^^ not found in `sdl2`
I tried with use sdl2::*; and with cargo +nightly run, but neither removed the error.
The documentation you are reading is is not for the version you are using. Build it yourself (cargo doc --open) or view it on docs.rs.
That function was added recently and has not been released yet. Perhaps you should file an issue for the authors of the crate to let them know that having documentation that doesn't correspond to any released code is confusing.
In the meantime, you can use a git dependency if you really need it.

"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.

Resources