Questions tagged [rust-result]
32 questions
2
votes
1 answer
How do I generify the error of a Rust Result to Result>?
I am trying to generify a Result that is returned by the reqwest::blocking::get function. It returns a Result but the function it is called in returns a Result

Dan
- 5,013
- 5
- 33
- 59
2
votes
1 answer
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> {
let mut primes: Vec = Vec::new();
for…

wellows
- 43
- 3
2
votes
1 answer
What does "T" stand for in Result in Rust?
The official documentation makes a lot of references to T:
enum Result {
Ok(T),
Err(E),
}
I gather that these are placeholder acronyms. While E should stand for "error", I'm not sure what T stands for.

Paul Razvan Berg
- 16,949
- 9
- 76
- 114
2
votes
1 answer
How can I return an error from Serde in a function that returns Result<() , Error>
I am attempting to return an error from Serde with a function that returns Result<(), Error>:
use std::io::{Error};
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Mine {
ab: u8,
ac: u8,
}
#[macro_use]
extern crate…

STF_ZBR
- 617
- 1
- 4
- 19
1
vote
2 answers
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(e) => return Err(e) and Err(e) => Err(e)?
Here is…

Peter
- 656
- 1
- 6
- 14
1
vote
0 answers
What is the closest thing to a Rust Result enum in JavaScript?
I am once again doing some Selenium automation in JavaScript and I have some methods that return boolean values about the webpage. For instance, I have a method isB2B() which returns whether the order form I'm automating is for a B2B order. It…

Benjamin Peinhardt
- 406
- 3
- 13
1
vote
2 answers
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> {
let val = std::env::args()
.nth(1)
.ok_or("1 arg is expected")
…

Midnight Exigent
- 615
- 4
- 15
1
vote
0 answers
How to work with custom string errors in rust?
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 {
println!("twenty");
} else {
Err("Not ten or…

Eka
- 14,170
- 38
- 128
- 212
0
votes
2 answers
Why is looping through a Devices object not returning Device objects?
I'm trying to Rust's cpal crate to enumerate a system's default host audio devices with the following code:
use cpal::traits::{HostTrait, DeviceTrait};
use cpal::{Device, Host};
fn main(){
let default_host:Host = cpal::default_host();
for…

Tadgh Wagstaff
- 93
- 5
0
votes
1 answer
Is there a simple way to get Result<&T, E> from Result?
Result.as_ref() will convert it to Result<&T, &E>, but the expected result is Result<&T, E>.

progquester
- 1,228
- 14
- 23
0
votes
1 answer
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 do is create a Signed JWT that contains a header…

Michael
- 174
- 2
- 13
0
votes
0 answers
Declaring a lazy_static as the Ok() result of a function that returns a Result<>
The ideal result would be something like this:
lazy_static! {
static ref LATESTGAMEDATA: god_rss::RSSGameData = async {
println!("Loading latest game data...");
god_rss::get_latest_game_data().await.unwrap()
…

steamsy
- 104
- 1
- 8
0
votes
1 answer
How to take T out of Result>>?
I'd like to take SomeType out of Result>>, and then pass it by a channel, but I failed:
pub fn read(result: Result>, ()>, tx: mpsc::Sender) {
if let Ok(datas) = result {
for data in…

xc wang
- 318
- 1
- 3
- 14
0
votes
1 answer
How to reuse a `Result`?
The following does not compile. What's the canonical way to make this work?
let file = File::open(&args.path)?;
let reader = BufReader::new(file);
for line in reader.lines() {
if line?.contains(&args.pattern) {
println!("{}", line?);
…

CXJ
- 4,301
- 3
- 32
- 62
0
votes
1 answer
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 == right)`
left: `"uh-oh!"`,
right: `"cannot parse…

Evan Carroll
- 78,363
- 46
- 261
- 468