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

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!

Related

Can't build RustDesk - Android

I tried to build RustDesk project following documentation at documentation - android build
After executing:
VCPKG_ROOT=/opt/vcpkg ANDROID_NDK_HOME=/opt/android-ndk-r22b flutter/ndk_arm64.sh
I cp-ed vcpkg and android-ndk-r22b from home to /opt to make sure that path is right
I'm getting this one error message:
[2023-01-13T09:59:47Z INFO cargo_ndk::cli] Using NDK at path: /opt/android-ndk-r22b (ANDROID_NDK_HOME)
[2023-01-13T09:59:47Z INFO cargo_ndk::cli] NDK API level: 21
[2023-01-13T09:59:47Z INFO cargo_ndk::cli] Building targets: arm64-v8a
[2023-01-13T09:59:47Z INFO cargo_ndk::cli] Building arm64-v8a (aarch64-linux-android)
warning: function `patch` is never used
--> libs/hbb_common/src/config.rs:239:4
|
239 | fn patch(path: PathBuf) -> PathBuf {
| ^^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: `hbb_common` (lib) generated 1 warning
Compiling rustdesk v1.2.0 (/home/mwitek/rustdesk)
error: failed to run custom build command for `rustdesk v1.2.0 (/home/mwitek/rustdesk)`
Caused by:
process didn't exit successfully: `/home/mwitek/rustdesk/target/release/build/rustdesk-9caa4dac4ffa2754/build-script-build` (exit status: 101)
--- stdout
cargo:rustc-link-search=/opt/vcpkg/installed/arm64-android/lib
cargo:rustc-link-lib=oboe
cargo:rustc-link-lib=c++
cargo:rustc-link-lib=OpenSLES
cargo:rerun-if-changed=src/flutter_ffi.rs
--- stderr
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Failed to run build_runner for /home/mwitek/rustdesk/flutter: ', build.rs:108:56
stack backtrace:
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
And to be honest I don't know how to get rid of it, I already tried to understand what is wrong from code-side but I can only find this section in build.rs:
fn install_oboe() {
let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap();
if target_os != "android" {
return;
}
let mut target_arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap();
if target_arch == "x86_64" {
target_arch = "x64".to_owned();
} else if target_arch == "aarch64" {
target_arch = "arm64".to_owned();
} else {
target_arch = "arm".to_owned();
}
let target = format!("{}-android", target_arch);
let vcpkg_root = std::env::var("VCPKG_ROOT").unwrap();
let mut path: std::path::PathBuf = vcpkg_root.into();
path.push("installed");
path.push(target);
println!(
"{}",
format!(
"cargo:rustc-link-search={}",
path.join("lib").to_str().unwrap()
)
);
println!("cargo:rustc-link-lib=oboe");
println!("cargo:rustc-link-lib=c++");
println!("cargo:rustc-link-lib=OpenSLES");
// I always got some strange link error with oboe, so as workaround, put oboe.cc into oboe src: src/common/AudioStreamBuilder.cpp
// also to avoid libc++_shared not found issue, cp ndk's libc++_shared.so to jniLibs, e.g.
// ./flutter_hbb/android/app/src/main/jniLibs/arm64-v8a/libc++_shared.so
// let include = path.join("include");
//cc::Build::new().file("oboe.cc").include(include).compile("oboe_wrapper");
}
But it doesn't look like anything for me
Also here is function that's generating exception:
fn gen_flutter_rust_bridge() {
let llvm_path = match std::env::var("LLVM_HOME") {
Ok(path) => Some(vec![path]),
Err(_) => None,
};
// Tell Cargo that if the given file changes, to rerun this build script.
println!("cargo:rerun-if-changed=src/flutter_ffi.rs");
// settings for fbr_codegen
let opts = lib_flutter_rust_bridge_codegen::Opts {
// Path of input Rust code
rust_input: "src/flutter_ffi.rs".to_string(),
// Path of output generated Dart code
dart_output: "flutter/lib/generated_bridge.dart".to_string(),
// Path of output generated C header
c_output: Some(vec!["flutter/macos/Runner/bridge_generated.h".to_string()]),
// for other options lets use default
llvm_path,
..Default::default()
};
// run fbr_codegen
lib_flutter_rust_bridge_codegen::frb_codegen(opts).unwrap();
}
Where it's last line is the one that throws this error.
lib_flutter_rust_bridge_codegen::frb_codegen(opts).unwrap();
I know this question really specific but documentation and self-investigation does not helping.
Also my OS properties: Ubuntu 20.04 x64 [Freshly installed]

Rust plotters doesn't draw

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

I can not run Substrate on M1

I could run Substrate before based on this advise. Link
cargo.toml
[patch.crates-io]
librocksdb-sys = { git =
"https://github.com/hdevalence/rustrocksdb", branch = "master" }
[workspace]
members = [
'node',
'pallets/*',
'runtime',
]
[profile.release]
panic = 'unwind'
cargo.lock
[[package]]
name = "libc"
version = "0.2.90"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum
="ba4aede83fc3617411dc6993bc8c70919750c1c257c6ca6a502aed6e0e2394ae"
But I got another error again. Like
169 | (*cx.uc_mcontext).__ss.__rip as *const u8
| ^^^^^ unknown field
|
= note: available fields are: `__x`, `__fp`, `__lr`, `__sp`, `__pc` ... and 2 others
error: aborting due to previous error
For more information about this error, try `rustc --explain E0609`.
error: could not compile `wasmtime-runtime`
To learn more, run the command again with --verbose.
warning: build failed, waiting for other jobs to finish...
error: build failed
Do you know how to resolve this problem? Thank you so much!

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) ✗

Resources