Rust plotters doesn't draw - rust

I'm trying to run one of the examples from rust's plotters user guide. For some reason, whenI run the example, no image is written out to images/. I looked through the whole project structure, no images are written to examples/ or otherwise.
Here's the output from cargo run:
plotters % cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.06s
Running `target/debug/plotters`
Here's the code:
use plotters::prelude::*;
fn main() {
let root_drawing_area = BitMapBackend::new("images/0.1.png", (1024, 768))
.into_drawing_area();
root_drawing_area.fill(&WHITE).unwrap();
let mut chart = ChartBuilder::on(&root_drawing_area)
.build_cartesian_2d(-3.14..3.14, -1.2..1.2)
.unwrap();
chart.draw_series(LineSeries::new(
(-314..314).map(|x| x as f64 / 100.0).map(|x| (x, x.sin())),
&RED
)).unwrap();
root_drawing_area.present().unwrap();
}
Am I missing something?
Edit:
I added the explicit call to root_drawing_area.present().unwrap();, now I get a helpful error message:
% cargo run
Compiling plotters v0.1.0 (/Users/sledge/Sandbox/plotters)
Finished dev [unoptimized + debuginfo] target(s) in 0.82s
Running `target/debug/plotters`
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: BackendError(DrawingError(ImageError(IoError(Os { code: 2, kind: NotFound, message: "No such file or directory" }))))', src/main.rs:18:33
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Related

Compiling bevy_dylib v0.5.0 error: linking with `cc` failed: exit status: 1

On Mac freshly upgraded to Monterey, I'm getting the following when attempting to cargo run a trivial Bevy program. I've reinstalled XCode CLTs like recommended here and other places. I've tried messing around with some of the cargo.yml with no success.
Compiling bevy_dylib v0.5.0
error: linking with `cc` failed: exit status: 1
note: "cc" "-Wl,-exported_symbols_list,/var/folders/rp/lky8r76j5v5dk0rqg_yzk35w0000gn/T/rustcDYBmaq/list"
"-m64" "-arch" "x86_64" <......... many pages of warnings>
ld: warning: object file (/Users/BWStearns/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy-glsl-to-spirv-0.2.1/build/osx/libSPIRV-Tools-opt.glsltospirv.a(const_folding_rules.cpp.o)) was built for newer macOS version (10.13) than being linked (10.7)
<many more pages of warnings>
"_CGDisplayCreateUUIDFromDisplayID", referenced from:
_$LT$winit..platform_impl..platform..monitor..MonitorHandle$u20$as$u20$core..cmp..PartialEq$GT$::eq::h541b069daf520ec9 in libwinit-4f8b93ad49cc21f8.rlib(winit-4f8b93ad49cc21f8.winit.355ded65-cgu.6.rcgu.o)
_$LT$winit..platform_impl..platform..monitor..MonitorHandle$u20$as$u20$core..cmp..Ord$GT$::cmp::h951d61f6bd1d7a5f in libwinit-4f8b93ad49cc21f8.rlib(winit-4f8b93ad49cc21f8.winit.355ded65-cgu.6.rcgu.o)
winit::platform_impl::platform::monitor::MonitorHandle::ns_screen::h3207f8aed4eae22f in libwinit-4f8b93ad49cc21f8.rlib(winit-4f8b93ad49cc21f8.winit.355ded65-cgu.6.rcgu.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: could not compile `bevy_dylib` due to previous error
The cargo.yml is like this:
[package]
name = "my_bevy_game"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
bevy = { version = "0.5.0", features = ["dynamic"] }
I just ran into this starting this week. I am pretty sure this exact code was compiling fine a couple weeks ago. Any help would be greatly appreciated.
main.rs looks like
use bevy::prelude::*;
struct Person;
struct Name(String);
fn add_people(mut commands: Commands) {
commands.spawn().insert(Person).insert(Name("Elaina Proctor".to_string()));
commands.spawn().insert(Person).insert(Name("Renzo Hume".to_string()));
commands.spawn().insert(Person).insert(Name("Zayna Nieves".to_string()));
}
fn greet_people(query: Query<&Name, With<Person>>) {
for name in query.iter() {
println!("hello {}!", name.0);
}
}
pub struct HelloPlugin;
impl Plugin for HelloPlugin {
fn build(&self, app: &mut AppBuilder) {
app.add_startup_system(add_people.system())
.add_system(greet_people.system());
}
}
fn main() {
App::build()
.add_plugins(DefaultPlugins)
.add_plugin(HelloPlugin)
.run();
}
Update:
Definitely something about the environment on my machine because on a different macbook it worked fine.
So I found a solution if you discover this issue and the code works on other machines. Uninstall rust with rustup self uninstall and then renistall with the standard script curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh and then you should be good.

What is the right way to use ink3 | self.env().block_timestamp() | Test Error :: 'uninitialized execution context: UninitializedBlocks'

What is the right way to use ink3 "self.env().block_timestamp()"
Reference code snippet ( full code is # https://gist.github.com/shamb0/a1f24cd7981e169cc5b7d1e1b3ec4dd4 )
pub fn get_bts(&self) -> u64 {
let bts = self.env().block_timestamp();
bts
}
Invoking test execution on this function ends up in the error panicked at 'uninitialized execution context: UninitializedBlocks'.
Complete Console trace
cargo +nightly test -- --nocapture
Finished test [unoptimized + debuginfo] target(s) in 0.04s
Running target/debug/deps/ink3_spad-ae1376ecf8f75a7e
running 3 tests
test ink3_spad::tests::default_works ... thread 'ok
ink3_spad::tests::bts_works' panicked at 'uninitialized execution context: UninitializedBlocks', $HOME/rusthome/.cargo/registry/src/github.com-1ecc6299db9ec823/ink_env-3.0.0-rc2/src/engine/off_chain/impls.rs:277:14test ink3_spad::tests::it_works ...
oknote: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
test ink3_spad::tests::bts_works ... FAILED
failures:
failures:
ink3_spad::tests::bts_works
test result: FAILED. 2 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out
error: test failed, to rerun pass '--lib'
Annotation must be "#[ink::test]" instead of #[test]
#[ink::test]
fn bts_works() {
let ink3_spad = Ink3Spad::new(false);
assert_eq!(ink3_spad.get(), false);
let dbg_fmtstr = format!("{:?}", ink3_spad.get_bts() );
ink_env::debug_println( &dbg_fmtstr );
}
Thanks #LaurentTrk for pointing me to the solution ...

Crate cannot find path

I am trying to use this crate to generate an ethereum address: https://docs.rs/ethkey/0.2.5/ethkey/
use ethkey::prelude::*;
fn main() {
let key = EthAccount::load_or_generate("~/", "passwd")
.expect("should load or generate new eth key");
println!("{:?}", key.address())
}
This is the example from the documentation and it doesnt seem to work.
I get the error below:
cargo run Compiling ethkey v0.1.0
(/Users/samueldare/Documents/Code/Thor/ethkey)
Finished dev [unoptimized + debuginfo] target(s) in 1.34s
Running target/debug/ethkey thread 'main' panicked at 'should load or generate new eth key: Error(IoError(Os { code: 2, kind:
NotFound, message: "No such file or directory" }), State { next_error:
None, backtrace: InternalBacktrace { backtrace: None } })',
src/libcore/result.rs:999:5 note: run with RUST_BACKTRACE=1
environment variable to display a backtrace.
Ive use ~/ as a last attempt to generate the key file in rust, but it still doesnt seem to work.
I will appreciate any pointers with this
The first argument to load_or_generate() takes a std::path::Path with no closing slash ( '/' ). Remove the slash:
fn main() {
let key = EthAccount::load_or_generate("~", "passwd")
.expect("should load or generate new eth key");
println!("{:?}", key.address())
}
Sample output:
05:47 ethtest (master) ✗ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.04s
Running `target/debug/ethtest`
Address(0x8248af6d1765e559509060b88e540a3567c42d20)
05:47 ethtest (master) ✗

simplelog::CombinedLogger panicking at runtime

I'm using the log and simplelog crates. Previously this code worked:
use simplelog::*;
CombinedLogger::init(
vec![
TermLogger::new(LevelFilter::Warn, Config::default()).unwrap(),
WriteLogger::new(LevelFilter::Info, Config::default(), File::create("main.log").unwrap()),
]
).unwrap();
But now it throws this error at runtime:
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', /checkout/src/libcore/option.rs:335:21
stack backtrace:
Cargo.toml dependencies:
log = "0.4.1"
simplelog = "0.5.0"
The documentation of TermLogger::new is not good, because it does not explain why it returns an Option.
I read the code: the function can fail because of term::stdout() or term::stderr(). Reading the documentation, your code fails because a terminal couldn't be opened.

"Unable to update registry: The server name or address could not be resolved" for a dependency

I am using Windows and facing below error. Not sure why it's not working.
C:\RUST\guess_game>cargo build
Updating registry `https://github.com/rust-lang/crates.io-index`
warning: spurious network error (2 tries remaining): [2/-1] failed to send request: The server name or address could not be resolved
warning: spurious network error (1 tries remaining): [2/-1] failed to send request: The server name or address could not be resolved
error: failed to load source for a dependency on `rand`
Caused by:
Unable to update registry https://github.com/rust-lang/crates.io-index
Caused by:
failed to fetch `https://github.com/rust-lang/crates.io-index`
Caused by:
[2/-1] failed to send request: The server name or address could not be resolved
You need internet access to download dependencies like rand:
Try these steps:
cargo new guessing_game --bin
cd guessing_game
cargo build
cargo run
Output:
C:\rust>cargo new guessing_game --bin
Created binary (application) `guessing_game` project
C:\rust>cd guessing_game
C:\rust\guessing_game>cargo build
Compiling guessing_game v0.1.0 (file:///C:/rust/guessing_game)
Finished dev [unoptimized + debuginfo] target(s) in 0.34 secs
C:\rust\guessing_game>cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
Running `target\debug\guessing_game.exe`
Hello, world!
Edit the Cargo.toml file:
[package]
name = "guessing_game"
version = "0.1.0"
authors = ["Your Name <you#example.com>"]
[dependencies]
rand = "0.3.14"
Output when there is no internet access:
C:\rust\guessing_game>cargo build
Updating registry `https://github.com/rust-lang/crates.io-index`
warning: spurious network error (2 tries remaining): [2/-1] failed to send request: A connection with the serve
r could not be established
warning: spurious network error (1 tries remaining): [2/-1] failed to send request: A connection with the server could not be established
error: failed to load source for a dependency on `rand`
Caused by:
Unable to update registry https://github.com/rust-lang/crates.io-index
Caused by:
failed to fetch `https://github.com/rust-lang/crates.io-index`
Caused by:
[2/-1] failed to send request: A connection with the server could not be established
Output with internet access. This takes some time and downloads 65MB of data for me:
C:\rust\guessing_game>cargo build
Updating registry `https://github.com/rust-lang/crates.io-index`
Downloading rand v0.3.16
Downloading libc v0.2.29
Compiling libc v0.2.29
Compiling rand v0.3.16
Compiling guessing_game v0.1.0 (file:///C:/rust/guessing_game)
Finished dev [unoptimized + debuginfo] target(s) in 4.35 secs
Use the crate in main.rs:
extern crate rand;
use std::io;
use std::cmp::Ordering;
use rand::Rng;
fn main() {
println!("Guess the number!");
let secret_number = rand::thread_rng().gen_range(1, 101);
loop {
println!("Please input your guess.");
let mut guess = String::new();
io::stdin()
.read_line(&mut guess)
.expect("Failed to read line");
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => continue,
};
println!("You guessed: {}", guess);
match guess.cmp(&secret_number) {
Ordering::Less => println!("Too small!"),
Ordering::Greater => println!("Too big!"),
Ordering::Equal => {
println!("You win!");
break;
}
}
}
}
Output:
C:\rust\guessing_game>cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
Running `target\debug\guessing_game.exe`
Guess the number!
Please input your guess.
50
You guessed: 50
Too small!
Please input your guess.
75
You guessed: 75
Too big!
Please input your guess.
60
You guessed: 60
Too big!
Please input your guess.
55
You guessed: 55
Too small!
Please input your guess.
57
You guessed: 57
You win!

Resources