<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Niranjana</title>
    <description>The latest articles on Forem by Niranjana (@niran_jana).</description>
    <link>https://forem.com/niran_jana</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F286990%2F717ea0ff-3e04-4ee0-a1dd-337f54f4fc6a.jpg</url>
      <title>Forem: Niranjana</title>
      <link>https://forem.com/niran_jana</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/niran_jana"/>
    <language>en</language>
    <item>
      <title>Understanding Rust Closures</title>
      <dc:creator>Niranjana</dc:creator>
      <pubDate>Fri, 07 Aug 2020 11:40:49 +0000</pubDate>
      <link>https://forem.com/niran_jana/understanding-rust-closures-42dh</link>
      <guid>https://forem.com/niran_jana/understanding-rust-closures-42dh</guid>
      <description>&lt;p&gt;Closures are very interesting programming concepts. It helps save a lot of expensive calculations while writing programs.&lt;br&gt;
Closure syntax of Rust is very similar to that of Ruby and SmallTalk.&lt;/p&gt;

&lt;p&gt;Rust’s closures are anonymous functions you can save in a variable or pass as arguments to other functions. You can create the closure in one place and then call the closure to evaluate it in a different context. Unlike functions, closures can capture values from the scope in which they’re defined. &lt;/p&gt;

&lt;p&gt;Closures are usually short and relevant only within a narrow context rather than in any arbitrary scenario. Within these limited contexts, the compiler is reliably able to infer the types of the parameters and the return type, similar to how it’s able to infer the types of most variables.&lt;/p&gt;

&lt;p&gt;Closures don’t require you to annotate the types of the parameters or the return value like fn functions do. Type annotations are required on functions because they’re part of an explicit interface exposed to your users. Defining this interface rigidly is important for ensuring that everyone agrees on what types of values a function uses and returns. But closures aren’t used in an exposed interface like this: they’re stored in variables and used without naming them and exposing them to users of our library.&lt;/p&gt;

&lt;p&gt;Let us dive into when to use closures. As mentioned in the definition, they are used at places when you need to call functions several times or repeat a value from a function call. &lt;/p&gt;

&lt;p&gt;Consider the following snippet. This piece helps to explain how closures work and most importantly how and where to apply closures.&lt;/p&gt;

&lt;pre&gt;


``` rust
 fn main() {
    let fact = |x| -&amp;gt; usize { (2..=x).product() };
    
    let five_fact = fact(5);
    
    println!("{}", five_fact);
    
    println!("{}", fact(8));

}

```


&lt;/pre&gt;

&lt;p&gt;One of the first functions we write in any programming language is that of a factorial calculator. The code above is pretty much self-explanatory. Instead of defining the function, we have used anonymous functions, i.e, closures.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>womenintech</category>
      <category>closures</category>
      <category>wecoded</category>
    </item>
    <item>
      <title>Minigrep : grep using Rust</title>
      <dc:creator>Niranjana</dc:creator>
      <pubDate>Thu, 23 Jul 2020 05:12:02 +0000</pubDate>
      <link>https://forem.com/niran_jana/minigrep-grep-using-rust-17k5</link>
      <guid>https://forem.com/niran_jana/minigrep-grep-using-rust-17k5</guid>
      <description>&lt;p&gt;grep (Global Regular Expression Print)is a command-line utility for searching plain-text data sets for lines that match a regular expression.&lt;br&gt;
Minigrep is a simple rust program that recreates the basic functions of grep alongside providing few more addition features:&lt;br&gt;
1.Case-insensitive search can be done by passing an option.&lt;br&gt;
2.Ability to do exact-match search.&lt;br&gt;
3.The query is highlighted in the output.&lt;/p&gt;

&lt;p&gt;The aim of the program is to be able to search for word in a given a text file. It goes by the following format:&lt;/p&gt;

&lt;pre&gt;


```shell
CASE_INSENSITIVE=1 cargo run “word to be searched” “filename” &amp;gt; “output file”
```


&lt;/pre&gt;

&lt;p&gt;Miniproject follows Test-driven development (TDD) process. This is a software development technique that uses the method of writing a test that fails for a reason expected and later modifying the code to make new test pass. This is followed by refactoring the code and continuing to ensure that the tests pass. Writing the test before you write the code that makes the test pass helps to maintain high test coverage throughout the process.&lt;br&gt;
The logic behind Minigrep can be explained with its main struct and run function.&lt;/p&gt;

&lt;pre&gt;


```rust
//struct
pub struct Config {
pub query: String,
pub filename: String,
pub case_sensitive: bool,
}
//run function
pub fn run(config: Config) -&amp;gt; Result&amp;gt; {
let contents = fs::read_to_string(config.filename)?;
let results = if config.case_sensitive {
search(&amp;amp;config.query, &amp;amp;contents)
} else {
search_case_insensitive(&amp;amp;config.query, &amp;amp;contents)
};
for line in results {
println!(“{}”, line);
}
Ok(())
}
```


&lt;/pre&gt;

&lt;p&gt;The program can be broken down into 5 parts:&lt;br&gt;
1.Accepting command line arguments&lt;br&gt;
2.Reading a file&lt;br&gt;
3.Writing tests an developing library&lt;br&gt;
4.Setting up environment variable&lt;br&gt;
5.Writing error messages to standard error&lt;br&gt;
6.Throughout these steps, we use refactoring to make the&lt;br&gt;
  program more efficient.&lt;br&gt;
For instance, the program began with the basic lines of code&lt;/p&gt;

&lt;pre&gt;


```rust
use std::env;
fn main() {
 let args: Vec = env::args().collect();
let query = &amp;amp;args[1];
let filename = &amp;amp;args[2];
println!(“Searching for {}”, query);
 println!(“In file {}”, filename);
}
```


&lt;/pre&gt;

&lt;p&gt;This was further developed into a struct seeing repetitions. The query and filename along with case sensitive boolean were added to the structure Config.&lt;br&gt;
To see more of how the logic panned into run() function, the tests and library along with directions of use, please find the source code on &lt;a href="https://github.com/niranjana687/minigrep"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>commandline</category>
      <category>womenintech</category>
      <category>grep</category>
    </item>
  </channel>
</rss>
