Posts with tag rust

rsproxy-sparse

2024-07-30
mirrorsrust

rsproxy-sparse??? info "Problem environment"- author: Julyfun MacOS14.5 M1 - edited date: 2024-07-30 - expected environment: Darwin floriandeMacBook-Air.local 23.5.0 Darwin Kernel Version 23.5.0: Wed May 1 20:16:51 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T8103 arm64Details of the problem / Steps to reproduce the errorNo detailsAnswer 1??? info "Answer environment"- author: As in the problem - edited date: As in the problem - verified environment: As in the problemTLDRin ~/.cargo/config.toml[source.crates-io] replace-with = 'rsproxy-sparse' [source.rsproxy] registry = "https://rsproxy.cn/crates.io-index" [source.rsproxy-sparse] registry = "sparse+https://rsproxy.cn/index/" [registries.rsproxy] index = "https://rsproxy.cn/crates.io-index" [net] git-fetch-with-cli = trueref: https://rsproxy.cn

To show output

2024-04-28
langsrusttest

ref: https://stackoverflow.com/questions/54585804/how-to-run-a-specific-unit-test-in-rustrustc --test <file>This generates a binary file. Then if you run the binary file, all tests will be conducted. Source code format be like:#[cfg(test)] mod tests { use super::*; #[test] fn test_tuple_out_of_range_positive() { assert_eq!( Color::try_from((256, 1000, 10000)), Err(IntoColorError::IntConversion) ); }To run a specific test, run:./xxx --exact test::test_tuple_out_of_range_positiveCargo waycargo test test_fn_name # filters with test_fn_name cargo test test_fn_name -- --exact cargo test test_mod_name::test_fn_name -- --exact cargo test --package school_info repeat_students_should_not_get_full_marks -- --exact # To show output cargo test --package py-like --test io -- tests::main --exact --nocapture # test1() in helper.rs cargo test helper::test1 -- --exac

classic-structure

2024-03-28
langspackagerust

ref: https://course.rs/basic/crate-module/crate.html典型的 Package 结构上面创建的 Package 中仅包含 src/main.rs 文件,意味着它仅包含一个二进制同名包 my-project。如果一个 Package 同时拥有 src/main.rs 和 src/lib.rs,那就意味着它包含两个包:库包和二进制包,这两个包名也都是 my-project —— 都与 Package 同名。一个真实项目中典型的 Package,会包含多个二进制包,这些包文件被放在 src/bin 目录下,每一个文件都是独立的二进制包,同时也会包含一个库包,该包只能存在一个 src/lib.rs:. ├── Cargo.toml ├── Cargo.lock ├── src │ ├── main.rs │ ├── lib.rs │ └── bin │ └── main1.rs │ └── main2.rs ├── tests │ └── some_integration_tests.rs ├── benches │ └── simple_bench.rs └── examples └── simple_example.rs唯一库包:src/lib.rs默认二进制包:src/main.rs,编译后生成的可执行文件与 Package 同名其余二进制包:src/bin/main1.rs 和 src/bin/main2.rs,它们会分别生成一个文件同名的二进制可执行文件集成测试文件:tests 目录下基准性能测试 benchmark 文件:benches 目录下项目示例:examples 目录下这种目录结构基本上是 Rust 的标准目录结构,在 GitHub 的大多数项目上,你都将看到它的身影

default-parameters

2024-03-27
langsrust

ref: https://www.thecodedmessage.com/posts/default-params/Sol 1impl Default for WindowConfig { fn default() -> Self { Self { width: 100, height: 100, visibility: WindowVisibility::Visible, window_style: WindowStyle::Standard, z_position: -1, autoclose: AutoclosePolicy::Disable, } } } let handle = create_window(WindowConfig { width: 500, z_position: 2, autoclose: AutoclosePolicy::Enable, ..Default::default() });Sol 2..impl WindowBuilder { fn height(mut self, height: u32) -> Self { self.height = height; self } // ... } impl WindowBuilder { fn height(self, height: u32) -> Self { Self { height, ..self } } // ... } impl WindowBuilder { fn autoclose_enable(mut self) -> Self { self.autoclose = AutoclosePolicy::Enable; self } fn autoclose_disable(mut self) -> Self { self.autoclose = AutoclosePolicy::Disable; self } } impl WindowBuilder { fn build(self) { window_create(self) } } let handle = WindowBuilder::new() .width(500) .z_position(2) .autoclose_enable() .build()

thread-spawn

2024-03-06
exampleslangsrust

use std::thread; fn is_prime(n: i32) -> bool { let mut result = true; for k in 2..(n as f32).powf(0.5) as i32 + 1 { if n % k == 0 { result = false; break; } } result } fn count_primes(start: i32, end: i32) -> i32 { let mut count = 0; for k in start..end { if is_prime(k) { count += 1; } } count } fn main() { const THREAD_COUNT: usize = 15; // 设置线程数量 let n = 1e8 as i32; let chunk_size = n / THREAD_COUNT as i32; let mut handles = vec![]; for i in 0..THREAD_COUNT { let start = i as i32 * chunk_size + 2; let end = if i == THREAD_COUNT - 1 { n } else { (i + 1) as i32 * chunk_size + 2 }; let handle = thread::spawn(move || count_primes(start, end)); handles.push(handle); } let mut total_count = 0; for handle in handles { total_count += handle.join().unwrap(); } println!("{}", total_count);

attempted-ssh-agent-authentication

2024-03-04
cargoinstalllangsrust

✗ cargo build Updating crates.io index error: failed to get `async-std` as a dependency of package `app-name v0.1.0 (/Users/sathia/Developer/rust-codebase/app-name)` Caused by: failed to load source for dependency `async-std` 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: failed to authenticate when downloading repository: [email protected]:rust-lang/crates.io-index * attempted ssh-agent authentication, but no usernames succeeded: `git` if the git CLI succeeds then `net.git-fetch-with-cli` may help here https://doc.rust-lang.org/cargo/reference/config.html#netgit-fetch-with-cli Caused by: no authentication availableSolref: https://stackoverflow.com/questions/62640383/how-to-make-gitconfigs-insteadof-work-with-cargoFor bash, export CARGO_NET_GIT_FETCH_WITH_CLI=true.For fish , set -gx CARGO_NET_GIT_FETCH_WITH_CLI true

No more posts to load.