Add comments to code in docs

This commit is contained in:
Sebastian Schmidt 2019-08-29 10:33:12 +03:00
parent 88559d5493
commit 3670bcaf63
2 changed files with 8 additions and 0 deletions

View File

@ -219,12 +219,16 @@ Here is a simple example to show the difference between preserving context betwe
use evalexpr::*;
assert_eq!(eval("a = 5;"), Ok(Value::from(())));
// The context is not preserved between eval calls
assert_eq!(eval("a"), Err(EvalexprError::VariableIdentifierNotFound("a".to_string())));
let mut context = HashMapContext::new();
assert_eq!(eval_with_context_mut("a = 5;", &mut context), Ok(Value::from(())));
// Assignments require mutable contexts
assert_eq!(eval_with_context("a = 6", &context), Err(EvalexprError::ContextNotManipulable));
// The HashMapContext is type safe
assert_eq!(eval_with_context_mut("a = 5.5", &mut context), Err(EvalexprError::ExpectedInt { actual: Value::from(5.5) }));
// Reading a variable does not require a mutable context
assert_eq!(eval_with_context("a", &context), Ok(Value::from(5)));
```

View File

@ -206,12 +206,16 @@
//! use evalexpr::*;
//!
//! assert_eq!(eval("a = 5;"), Ok(Value::from(())));
//! // The context is not preserved between eval calls
//! assert_eq!(eval("a"), Err(EvalexprError::VariableIdentifierNotFound("a".to_string())));
//!
//! let mut context = HashMapContext::new();
//! assert_eq!(eval_with_context_mut("a = 5;", &mut context), Ok(Value::from(())));
//! // Assignments require mutable contexts
//! assert_eq!(eval_with_context("a = 6", &context), Err(EvalexprError::ContextNotManipulable));
//! // The HashMapContext is type safe
//! assert_eq!(eval_with_context_mut("a = 5.5", &mut context), Err(EvalexprError::ExpectedInt { actual: Value::from(5.5) }));
//! // Reading a variable does not require a mutable context
//! assert_eq!(eval_with_context("a", &context), Ok(Value::from(5)));
//!
//! ```