What's wrong with this enum Option<T> code?

I'm new to rust. I copied this code from rust website documentation.
I tried to debug this but I think, it's from the compiler or invalid documentation.

fn main() {
    let greeting = Some("Hello, World");
    println!("{:?}", greeting);
    let some_number = Some(5);
    let some_string = Some("a string");
    let absent_number: Option<i32> = None;

enum Option<T> {



Some("Hello, World")


   Compiling playground v0.0.1 (/playground)
warning: unused variable: `some_number`
 --> src/main.rs:5:9
5 |     let some_number = Some(5);
  |         ^^^^^^^^^^^ help: consider prefixing with an underscore: `_some_number`
  = note: #[warn(unused_variables)] on by default

warning: unused variable: `some_string`
 --> src/main.rs:6:9
6 |     let some_string = Some("a string");
  |         ^^^^^^^^^^^ help: consider prefixing with an underscore: `_some_string`

warning: unused variable: `absent_number`
 --> src/main.rs:8:9
8 |     let absent_number: Option<i32> = Option::None;
  |         ^^^^^^^^^^^^^ help: consider prefixing with an underscore: `_absent_number`

warning: variant is never constructed: `Some`
  --> src/main.rs:13:5
13 |     Some(T),
   |     ^^^^^^^
   = note: #[warn(dead_code)] on by default

    Finished dev [unoptimized + debuginfo] target(s) in 0.79s
     Running `target/debug/playground`

This code compiles and runs well. What you get are not errors, but warnings, hinting about some ineffective or unidiomatic code - thing to consider in real code, but sometimes they are intentionally left in the examples, so that the code is more clearly describing the thing it should.

I was getting an error, i will upload a screenshot of the errors am getting.

Can you try doing Option::None ? Looks like namespace conflict. Note that stdlib Option and its variant are automatically imported into the global scope.

You don't need to define Option<T> yourself; it's in the standard library. In the book, we show the definition of Option<T> in order to explain it, but the next example with the rest of the code you have here doesn't need Option<T> to be redefined.

1 Like

It's working. Thanks
I'm still getting some warnings. I fixed some will get back to you on anyone am unable to fix. Great man

I'm working with None not the Some(T), i don't need to define it for Some(T) but i can for None. That what i saw in the docs but i will re-read the docs

To make sure I'm being clear, what I'm saying is remove this part from your code:

enum Option<T> {

Then it works: Playground

1 Like

Wow! it's works fine. Just like magic. Thanks

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.