From 3670bcaf63f67c23438b515d72bc6650cc89e9ab Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Thu, 29 Aug 2019 10:33:12 +0300 Subject: [PATCH] Add comments to code in docs --- README.md | 4 ++++ src/lib.rs | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/README.md b/README.md index 8e5c04f..4724065 100644 --- a/README.md +++ b/README.md @@ -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))); ``` diff --git a/src/lib.rs b/src/lib.rs index 9a63692..ab7fc4d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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))); //! //! ```