In general case, Result<T, E>, as it is stated by its definition, might be either the Ok(T), i.e. "operation executed successfully with result of type T", or Err(E), i.e. "operation returned error of type E". For std::io::Error, E is fixed and T is defined by the concrete function.
It has become convention in Rust to define "local" type aliases called Result that incorporate the local error type, in exactly this way that std::io does it. You'll also find the same in many crates, or in other type aliases called Result in other parts of libstd.
In the docs, you can quickly see the difference by looking at the color! Type aliases are orangeish and enums are green.
E is std::io::Error struct. consider my program here
/************************************
use std::fs::File;
fn main()
{
let fd1 = File::open("test1.txt");
let m = match fd1 {
Ok(file) => file,
Err(e) => {
println!("Hello Error");
},
};
println!("{:?}",m);
}
**********************/
I get compilation error. What I'm trying to do is, if I get error, I want to execute the print
statement. How to print my statement when I get error while opening the file ?
The compilation error is
--> simple3.rs:11:23
|
8 | let m = match fd1 {
| -
9 | |
10 | | Ok(file) => file,
| | ---- this is found to be of type std::fs::File
11 | | Err(e) => {
| |_________^
12 | || println!("Hello Error");
13 | || },
| ||____^ expected struct std::fs::File, found ()
14 | |
15 | |
16 | | };
| |- match arms have incompatible types
|
= note: expected type std::fs::File
found type ()
Looking into much basics....I have a following program
enum Message {
Quit,
Move { x: i32, y :i32} ,
Write (String),
Change (i32, i32, i32),
Value (i32),
}
struct Point {
x: i32,
y : i32,
}
fn main()
{
let m = Message::Move { x : 5, y :10};
let v = Message::Value(100);
let p = Point { x: 5, y:10};
println!("{}",p.x);
}
I could able to "get" the value of p which of type Point.. for example
I can get x component by using p.x
Similarly I want to assign the enumerator Value(5) to an i32 type and then print that.
How to do that ?
///
//I think I put the code in code block as told by Alice.
I'm relatively new to Rust so my questions some time may be too childish or very basics.
Kindly bear with me.
As Rust is a strongly-typed language it won't allow you change types implicitly. You can't assign a value of Message::Value(5) to a i32-variable as it is of type Message.
What you can do is to extract values out of an enum by using match. For example:
fn main() {
// Type annotations are not necessary but I include them for clarity.
let v: Message = Message::Value(5);
let x: i32 = match v { // look into 'v'
Message::Value(inner) => inner, // If it is a 'Message::Value' return its inner value - the 'i32'
_ => panic!(), // in every other case panic, i.e. abort, the program
};
println!("Value of x is: {}", x);
}
I hope this helped you.
As you pointed out these are really basic questions. I and many other people in this forum will be glad to help you if you have any further problems.
For a better understanding I strongly recommend you to give the Rust Book a try. It is online and for free. I think it is really well written and gives you a good introduction to the language and its concepts.
The value types , the variant or enumerator can hold is specified like a
tuple construct. For the first time, it looked like a function call (although only types are
mentioned here).
Can I say that variants holds tuple in this enum case ?. Is that abstraction is correct ?.