{"action":"created","issue":{"url":"https://api.github.com/repos/rust-lang/rfcs/issues/1416","labels_url":"https://api.github.com/repos/rust-lang/rfcs/issues/1416/labels{/name}","comments_url":"https://api.github.com/repos/rust-lang/rfcs/issues/1416/comments","events_url":"https://api.github.com/repos/rust-lang/rfcs/issues/1416/events","html_url":"https://github.com/rust-lang/rfcs/issues/1416","id":123028458,"number":1416,"title":"Add `give` method to `Option<T>`","user":{"login":"Undeterminant","id":1756968,"avatar_url":"https://avatars.githubusercontent.com/u/1756968?v=3","gravatar_id":"","url":"https://api.github.com/users/Undeterminant","html_url":"https://github.com/Undeterminant","followers_url":"https://api.github.com/users/Undeterminant/followers","following_url":"https://api.github.com/users/Undeterminant/following{/other_user}","gists_url":"https://api.github.com/users/Undeterminant/gists{/gist_id}","starred_url":"https://api.github.com/users/Undeterminant/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Undeterminant/subscriptions","organizations_url":"https://api.github.com/users/Undeterminant/orgs","repos_url":"https://api.github.com/users/Undeterminant/repos","events_url":"https://api.github.com/users/Undeterminant/events{/privacy}","received_events_url":"https://api.github.com/users/Undeterminant/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/rust-lang/rfcs/labels/A-enhancement","name":"A-enhancement","color":"d7e101"},{"url":"https://api.github.com/repos/rust-lang/rfcs/labels/A-libs","name":"A-libs","color":"d7e101"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":13,"created_at":"2015-12-18T21:21:48Z","updated_at":"2015-12-25T18:56:48Z","closed_at":null,"body":"Currently, `Option<T>` has the method `take`, which is implemented as `replace(&mut self, None)`. To complement this method, I propose adding a `give` as well:\r\n\r\n```\r\npub fn give(&mut self, val: T) -> Option<T> {\r\n    match *self {\r\n        None => ::std::mem::replace(&mut self, Some(val)),\r\n        Some(_) => Some(val),\r\n    }\r\n}\r\n\r\n#[test]\r\nfn it_works() {\r\n    let mut opt = None;\r\n    assert_eq!(opt.give(5), None);\r\n    assert_eq!(opt, Some(5));\r\n    assert_eq!(opt.give(6), Some(6));\r\n    assert_eq!(opt, Some(5));\r\n}\r\n\r\n```\r\n\r\nThis follows the same standard that `HashMap`, `BTreeMap`, and other collections use. If the `Option` has no value, it puts it in the `Option` as a `Some` value and returns `None`, otherwise leaves the `Option` as it is and gives back the value as a `Some`.\r\n\r\nI feel like this is a common enough pattern to warrant a method from the standard library."},"comment":{"url":"https://api.github.com/repos/rust-lang/rfcs/issues/comments/167257787","html_url":"https://github.com/rust-lang/rfcs/issues/1416#issuecomment-167257787","issue_url":"https://api.github.com/repos/rust-lang/rfcs/issues/1416","id":167257787,"user":{"login":"leodasvacas","id":9885558,"avatar_url":"https://avatars.githubusercontent.com/u/9885558?v=3","gravatar_id":"","url":"https://api.github.com/users/leodasvacas","html_url":"https://github.com/leodasvacas","followers_url":"https://api.github.com/users/leodasvacas/followers","following_url":"https://api.github.com/users/leodasvacas/following{/other_user}","gists_url":"https://api.github.com/users/leodasvacas/gists{/gist_id}","starred_url":"https://api.github.com/users/leodasvacas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/leodasvacas/subscriptions","organizations_url":"https://api.github.com/users/leodasvacas/orgs","repos_url":"https://api.github.com/users/leodasvacas/repos","events_url":"https://api.github.com/users/leodasvacas/events{/privacy}","received_events_url":"https://api.github.com/users/leodasvacas/received_events","type":"User","site_admin":false},"created_at":"2015-12-25T18:56:48Z","updated_at":"2015-12-25T18:56:48Z","body":"I recently proposed something similar however I came to the conclusion that this would be a bad API because after being called it guarantees that the option will be `Some`, this encourages unwrapping and dosen't seem like a good use of `Option` after all."}}