28 questions
0 votes
1 answer
155 views
In Rust, what's the most idiomatic way to convert a result to another result given the Error type is convertible [closed]
Let's say I call a function that returns me Result<Foo, Error1> and want to return Result<Foo, Error2>. Error2 implements From<Error1>. The two options I have in mind are fn f1() -&...
0 votes
1 answer
107 views
Is there a simple way to get Result<&T, E> from Result<T, E>?
Result<T, E>.as_ref() will convert it to Result<&T, &E>, but the expected result is Result<&T, E>.
0 votes
1 answer
2k views
Read claims without verification. , Understanding the Result return
I'm working JWT's in Rust and have come across a situation that I'm having some issues navigating. I'm new to both Rust and JWT's, so bare with me. I'm using the jwt crate found here. What I want to ...
1 vote
2 answers
3k views
Why is `return` necessary in `match` arm when using `Result` in `main`?
I am reading Rust by Example book. In this example removing return in Err(e) => return Err(e) causes an error: expected `i32`, found enum `Result`` . Why is that? What is the difference between Err(...
0 votes
1 answer
473 views
How to take T out of Result<Vec<Data<&T>>>?
I'd like to take SomeType out of Result<Vec<Data<&SomeType>>>, and then pass it by a channel, but I failed: pub fn read(result: Result<Vec<Data<&SomeType>>, ()&...
4 votes
1 answer
5k views
How do I generify the error of a Rust Result<T, E> to Result<T, Box<dyn std::error::Error>>?
I am trying to generify a Result that is returned by the reqwest::blocking::get function. It returns a Result<reqwest::blocking::Response, reqwest::Error> but the function it is called in ...
14 votes
1 answer
7k views
What is the point of an Infallible Result, over just returning the Ok() branch?
The canonical example of a Warp rejection handler is async fn handle_rejection(err: Rejection) -> Result<impl Reply, Infallible> { But what's the advantage of a Result<ok, err> such ...
1 vote
2 answers
1k views
Should I ditch using `and_then` and always use the `?` operator?
I want to write something like this, but it won't compile due to a mismatch between types: fn main() -> std::result::Result<(), Box<dyn std::error::Error>> { let val = std::env::...
1 vote
0 answers
63 views
How to work with custom string errors in rust? [duplicate]
How can I return my own string errors in rust? fn main() -> std::io::Result<(), std::io::Error> { let a = 30; if a == 10 { println!("ten"); } else if a == 20 { ...
2 votes
1 answer
2k views
How do I return a Result data type in Rust?
I have the code below fn main() { let num: i64 = 600851475143; println!("Largest prime: {}", largest_prime_factor(num)); } fn largest_prime_factor(num:i64) -> Result<i64, ...
9 votes
1 answer
6k views
Calling map on Iter of Results in Rust
I would like to write some code in a "functional programming" style. However, I start with an Iterator of Results and I only want to apply the function to the Ok items. Furthermore, I want ...
4 votes
1 answer
846 views
How to avoid "Error:" output when returning Result from main?
I'm trying to make my own custom errors but I do not want Rust to automatically add Error: in front of the error messages. use std::error::Error; use std::fmt; #[derive(Debug)] enum CustomError { ...
7 votes
1 answer
11k views
Cannot call a function that returns Result: found opaque type impl std::future::Future
I cannot return a result of a function from a Result. Every tutorial only shows how to use a Result, but not how to return a value from it. fn main(){ let mut a: Vec<String> = Vec::new(); ...
13 votes
1 answer
4k views
What does dotenv().ok() do?
I am using the Diesel ORM wrapper with PostgreSQL. I was following the guide on their website which has the following code: pub fn establish_connection() -> PgConnection { dotenv().ok(); ...
0 votes
1 answer
798 views
Why doesn't Rustlings force me to consume a Result?
After a brief attempt, when I run the the Rustling test for exercises/error_handling/errorsn.rs, I get ---- test_ioerror stdout ---- thread 'test_ioerror' panicked at 'assertion failed: `(left == ...