Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
In Rust, it seems that the syntax for simple math expressions like sin(x)+cos(y)*sqrt(z) have to be written like x.sin()+y.cos()*z.sqrt(), which is weird, and anti-natural, and prone to errors with more complex expressions (exp, log, powers, etc.).
How is it possible to write math expressions the “classical” way? If not possible, why Rust is asking to write them that way?
As mentioned, these methods are defined on the type. A possible alternative is to call the method directly, e.g:
f64::sqrt(25.0);
If you really want to, you can also bind it like so:
fn example_a(n: f64) -> f64 {
let sqrt = f64::sqrt;
sqrt(25.0)
}
const SQRT: fn(f64)-> f64 = f64::sqrt;
fn example_b(n: f64) -> f64 {
SQRT(25.0)
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
self.0.iter().map(Some)
https://github.com/zcash/halo2/blob/main/halo2_proofs/src/circuit/floor_planner/v1/strategy.rs#L71
I tried to google some keywords, but didn't find any result.
There is some confusion, so let's clarify things. Some is not an enum (as you said in a comment), it's part of an enum, that is, the Option<T> type was defined as
enum Option<T> {
Some(T),
None,
}
Here, Some(T) is called a variant of this enum. However, for convenience, the compiler will produce a function with the same name, with the same signature, that simply produces that variant. That is, Some: fn(T) -> Option<T>. Similarly, the None variant produces a constant of the same name.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 months ago.
Improve this question
I'm trying to find a way how to document my tests which might be rather long to fully explain the context and desired behavior(up to 10-12 words).
I'm more used to BDD style specs with nested contexts and verbose expectations but standard [test] attribute is fine as well.
My question is: can this happy-path snippet below be somehow rewritten for better readability?
#[test]
fn test_when_user_this_and_than_it_does_something_special() {
// ...
}
I was looking for something like #[test(name="plain text test case description")](to avoid heave snake_case naming) but without much success. Or perhaps there is a crate to mitigate this issue?
Rust does not have any kind of test naming separate from the function names. In order to include information about the purpose of the test, I would suggest that you write a concise name and documentation containing the rest of the words:
/// When the user does this and that, the special thing should happen.
#[test]
fn this_that_then_special() {
// ...
}
You can also, if you want, put the comment inside the block using the inner doc-comment syntax //!:
#[test]
fn this_that_then_special() {
//! When the user does this and that, the special thing should happen.
// ...
}
Documentation for tests doesn't show up in generated documentation files, of course, so there isn't a whole lot of point to using the specific syntax, but it is a standard syntax for attaching an explanation to any item.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed last year.
The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved
Improve this question
I realize that I always use Result when I need to return something from a function.
Is it something usual in a good Rust development? And should I also return a Result for a void function that might encounter an error (like Result<(), Error>)?
A Result<T, E> should be always returned by "error-prone" functions, but not necessarily all functions.
std::fs::read_to_string(), for example, returns a Result because of the possibility of various errors occurring. vec.len(), on the other hand, is guaranteed to not raise an error (and as such directly returns a usize instead of a Result).
As for your second question, you're absolutely correct: error-prone (but non-value-returning) functions should return a Result<(), Error>.
Yes. Returning Result is very common in Rust.
In practice, this can often be shortened to Result<T> instead of Result<T, E>. I'm not saying that's always a good idea, but in many cases it is and can save some typing. Here is an example with the anyhow's Result type:
pub type Result<T, E = Error> = core::result::Result<T, E>;
If you have a "void" function that could return an error, you can declare the function as returning Result<()>.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
If you have an instance of std::path::Path and want to write a text File to that path, what is the best way? I can call .display() to get a string and then use File I/O methods that take strings as file names, but is there a preferred way that involves something related to Path, PathBuf, etc? The .display() method is said to be lossy due to encoding issues so I would prefer a means that did not sacrifice generality.
You can use std::fs::write. The function has following signature, so it will work with Path objects too.
pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> Result<()>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
A Vec<T> can be converted to a string:
let string: String = format!("{:.?}", vec));
Can said string be converted back into a Vec<T>?
No, not for generic Ts, as the output of Debug (e.g. via #[derive(Debug)]) does not necessarily yield the right format for FromStr (if the type even implements it). Furthermore, Vec does not implement FromStr, so parse() needs at least a wrapper around the Vec.
Also, while most Debug implementation (including the derived one) show the values of their fields, they're not required to. You cannot get the lost information back at that point.