summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorflywind <43030857+xflywind@users.noreply.github.com>2020-12-30 09:26:49 -0600
committerGitHub <noreply@github.com>2020-12-30 16:26:49 +0100
commit805917768dea1b223a3aab3131d5c76f862b4984 (patch)
treea591638fd906fc10f4e8bb076fc26addfb3edac9
parentb8658a3e242f7304f7b0320089ef5aa0d5375c25 (diff)
downloadNim-805917768dea1b223a3aab3131d5c76f862b4984.tar.gz
use runnableExamples in options (#16503)
-rw-r--r--lib/pure/options.nim43
1 files changed, 17 insertions, 26 deletions
diff --git a/lib/pure/options.nim b/lib/pure/options.nim
index 8de4430c1..195da1008 100644
--- a/lib/pure/options.nim
+++ b/lib/pure/options.nim
@@ -23,38 +23,29 @@
 ## Let's start with an example: a procedure that finds the index of a character
 ## in a string.
 ##
-## .. code-block:: nim
-##
-##   import options
-##
-##   proc find(haystack: string, needle: char): Option[int] =
-##     for i, c in haystack:
-##       if c == needle:
-##         return some(i)
-##     return none(int)  # This line is actually optional,
-##                       # because the default is empty
-##
-## .. code-block:: nim
-##
-##    let found = "abc".find('c')
-##    assert found.isSome and found.get() == 2
-##
+runnableExamples:
+  proc find(haystack: string, needle: char): Option[int] =
+    for i, c in haystack:
+      if c == needle:
+        return some(i)
+    return none(int)  # This line is actually optional,
+                      # because the default is empty
+
+  let found = "abc".find('c')
+  assert found.isSome and found.get() == 2
+
 ## The `get` operation demonstrated above returns the underlying value, or
 ## raises `UnpackDefect` if there is no value. Note that `UnpackDefect`
 ## inherits from `system.Defect`, and should therefore never be caught.
 ## Instead, rely on checking if the option contains a value with
 ## `isSome <#isSome,Option[T]>`_ and `isNone <#isNone,Option[T]>`_ procs.
-##
+## 
 ## How to deal with an absence of a value:
-##
-## .. code-block:: nim
-##
-##   let result = "team".find('i')
-##
-##   # Nothing was found, so the result is `none`.
-##   assert(result == none(int))
-##   # It has no value:
-##   assert(result.isNone)
+
+runnableExamples:
+  let result = none(int)
+  # It has no value:
+  assert(result.isNone)
 
 import typetraits
 
='n193' href='#n193'>193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216