rust

How to return Ok unit type of std::result<(), E>?

泪湿孤枕 提交于 2021-01-21 06:43:59
问题 If I define a function: fn f() -> Result<(), E> { // How to return Ok()? } How can I return the Ok in std::result with the unit type () ? 回答1: The only value of type () is () , so just put that inside the Ok constructor: fn f() -> Result<(), E> { Ok(()) } 回答2: Use Ok(()) as in fn f() -> Result<(), E> { Ok(()) } 来源: https://stackoverflow.com/questions/27945858/how-to-return-ok-unit-type-of-stdresult-e

Up to date list of available nightly features?

那年仲夏 提交于 2021-01-21 04:49:29
问题 Where can I find an up to date list of available nightly features that I can activate? Example: #![feature(plugin_registrar, rustc_private)] 回答1: The Unstable Book has a list of features, but some features are not documented there. A complete list of features is contained in the source code (or the current master branch). To date, active features are: declare_features! ( // ------------------------------------------------------------------------- // feature-group-start: internal feature gates

How do I use Wasm in the content script of a Firefox web extension?

北慕城南 提交于 2021-01-21 04:41:26
问题 I am building a Firefox addon using Rust. I am trying to insert HTML and do stuff on specific pages. Apparently, a content script is the thing I want to use. My content script is: import("../crate/pkg").then(({ Addon }) => { const addon = Addon.new(); console.log(addon.where_am_i()); }).catch(e => console.error("Error importing:", e)); The error I am getting is: TypeError: "0125c9960050e7483877.module.wasm is not a valid URL." I tried to add to manifest.json : "web_accessible_resources": [

How can I specify which crate `cargo run` runs by default in the root of a Cargo workspace?

二次信任 提交于 2021-01-20 19:30:25
问题 Right now I have a Cargo workspace with three members. [workspace] members = [ "foo", "bar", "baz", ] If I run cargo run in the root directory, I get this error: error : manifest path /home/lukas/dev/mahboi/Cargo.toml is a virtual manifest, but this command requires running against an actual package in this workspace That makes sense. I can run cargo run -p foo and it works. But the thing is: foo is the only crate that is executable and I will execute it very often, so it would be nice if I

How can I specify which crate `cargo run` runs by default in the root of a Cargo workspace?

五迷三道 提交于 2021-01-20 19:29:19
问题 Right now I have a Cargo workspace with three members. [workspace] members = [ "foo", "bar", "baz", ] If I run cargo run in the root directory, I get this error: error : manifest path /home/lukas/dev/mahboi/Cargo.toml is a virtual manifest, but this command requires running against an actual package in this workspace That makes sense. I can run cargo run -p foo and it works. But the thing is: foo is the only crate that is executable and I will execute it very often, so it would be nice if I

Understanding the Send trait

断了今生、忘了曾经 提交于 2021-01-20 19:20:45
问题 I am trying to wrap my head around Send + Sync traits. I get the intuition behind Sync - this is the traditional thread safety(like in C++ ). The object does the necessary locking(interior mutability if needed), so threads can safely access it. But the Send part is bit unclear. I understand why things like Rc are Send only - the object can be given to a different thread, but non-atomic operations make it thread unsafe. What is the intuition behind Send ? Does it mean the object can be copied

Understanding the Send trait

 ̄綄美尐妖づ 提交于 2021-01-20 19:19:35
问题 I am trying to wrap my head around Send + Sync traits. I get the intuition behind Sync - this is the traditional thread safety(like in C++ ). The object does the necessary locking(interior mutability if needed), so threads can safely access it. But the Send part is bit unclear. I understand why things like Rc are Send only - the object can be given to a different thread, but non-atomic operations make it thread unsafe. What is the intuition behind Send ? Does it mean the object can be copied

How to check release / debug builds using cfg in Rust?

此生再无相见时 提交于 2021-01-20 17:00:08
问题 With the C pre-processor it's common to do, #if defined(NDEBUG) // release build #endif #if defined(DEBUG) // debug build #endif Cargo's rough equivalents are: cargo build --release for release. cargo build for debug. How would Rust's #[cfg(...)] attribute or cfg!(...) macro be used to do something similar? I understand that Rust's pre-processor doesn't work like C's. I checked the documentation and this page lists some attributes. (assuming this list is comprehensive) debug_assertions could

Why is Rust's assert_eq! implemented using a match?

ぃ、小莉子 提交于 2021-01-20 15:25:26
问题 Here's Rust's assert_eq! macro implementation. I've copied only the first branch for brevity: macro_rules! assert_eq { ($left:expr, $right:expr) => ({ match (&$left, &$right) { (left_val, right_val) => { if !(*left_val == *right_val) { panic!(r#"assertion failed: `(left == right)` left: `{:?}`, right: `{:?}`"#, left_val, right_val) } } } }); } What's the purpose of the match here? Why isn't checking for non-equality enough? 回答1: Alright, let's remove the match. macro_rules! assert_eq_2 { (

Why is Rust's assert_eq! implemented using a match?

99封情书 提交于 2021-01-20 15:24:48
问题 Here's Rust's assert_eq! macro implementation. I've copied only the first branch for brevity: macro_rules! assert_eq { ($left:expr, $right:expr) => ({ match (&$left, &$right) { (left_val, right_val) => { if !(*left_val == *right_val) { panic!(r#"assertion failed: `(left == right)` left: `{:?}`, right: `{:?}`"#, left_val, right_val) } } } }); } What's the purpose of the match here? Why isn't checking for non-equality enough? 回答1: Alright, let's remove the match. macro_rules! assert_eq_2 { (