]> git.lizzy.rs Git - rust.git/blob - tests/ui/question_mark.rs
Merge pull request #3291 from JoshMcguigan/cmp_owned-3289
[rust.git] / tests / ui / question_mark.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10
11 fn some_func(a: Option<u32>) -> Option<u32> {
12         if a.is_none() {
13                 return None
14         }
15
16         a
17 }
18
19 pub enum SeemsOption<T> {
20     Some(T),
21     None
22 }
23
24 impl<T> SeemsOption<T> {
25     pub fn is_none(&self) -> bool {
26         match *self {
27             SeemsOption::None => true,
28             SeemsOption::Some(_) => false,
29         }
30     }
31 }
32
33 fn returns_something_similar_to_option(a: SeemsOption<u32>) -> SeemsOption<u32> {
34     if a.is_none() {
35         return SeemsOption::None;
36     }
37
38     a
39 }
40
41 pub struct SomeStruct {
42         pub opt: Option<u32>,
43 }
44
45 impl SomeStruct {
46         pub fn func(&self) -> Option<u32> {
47                 if (self.opt).is_none() {
48                         return None;
49                 }
50
51                 self.opt
52         }
53 }
54
55 fn main() {
56         some_func(Some(42));
57         some_func(None);
58
59         let some_struct = SomeStruct { opt: Some(54) };
60         some_struct.func();
61
62     let so = SeemsOption::Some(45);
63     returns_something_similar_to_option(so);
64 }