Skip to content

Commit

Permalink
Deploying to gh-pages from @ 0f89483 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
damoasda committed Nov 26, 2023
1 parent 4c8f662 commit 1c7598b
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 124 deletions.
45 changes: 15 additions & 30 deletions ch12-04-testing-the-librarys-functionality.html
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,7 @@ <h3 id="schreiben-eines-fehlschlagenden-tests"><a class="header" href="#schreibe
und gibt nur die Zeilen aus dem Text zurück, die die Suchabfrage enthalten.
Codeblock 12-15 zeigt diesen Test, der sich noch nicht kompilieren lässt.</p>
<p><span class="filename">Dateiname: src/lib.rs</span></p>
<pre><pre class="playground"><code class="language-rust does_not_compile"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span><span class="boring">use std::error::Error;
<pre><code class="language-rust ignore does_not_compile"><span class="boring">use std::error::Error;
</span><span class="boring">use std::fs;
</span><span class="boring">
</span><span class="boring">pub struct Config {
Expand All @@ -210,7 +208,7 @@ <h3 id="schreiben-eines-fehlschlagenden-tests"><a class="header" href="#schreibe
</span><span class="boring">}
</span><span class="boring">
</span><span class="boring">impl Config {
</span><span class="boring"> pub fn new(args: &amp;[String]) -&gt; Result&lt;Config, &amp;'static str&gt; {
</span><span class="boring"> pub fn build(args: &amp;[String]) -&gt; Result&lt;Config, &amp;'static str&gt; {
</span><span class="boring"> if args.len() &lt; 3 {
</span><span class="boring"> return Err(&quot;Nicht genügend Argumente&quot;);
</span><span class="boring"> }
Expand Down Expand Up @@ -242,8 +240,7 @@ <h3 id="schreiben-eines-fehlschlagenden-tests"><a class="header" href="#schreibe

assert_eq!(vec![&quot;sicher, schnell, produktiv.&quot;], search(query, contents));
}
}
<span class="boring">}</span></code></pre></pre>
}</code></pre>
<p><span class="caption">Codeblock 12-15: Erstellen eines fehlschlagenden Tests
für die Funktion <code>search</code>, die wir uns wünschen</span></p>
<p>Dieser Test sucht nach der Zeichenkette <code>&quot;dukt&quot;</code>. Der Text, den wir
Expand Down Expand Up @@ -401,9 +398,7 @@ <h4 id="iterieren-über-zeilen-mit-der-methode-lines"><a class="header" href="#i
bequemerweise <code>lines</code> genannt, die wie in Codeblock 12-17 gezeigt funktioniert.
Beachte, dass dies noch nicht kompiliert.</p>
<p><span class="filename">Dateiname: src/lib.rs</span></p>
<pre><pre class="playground"><code class="language-rust does_not_compile"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span><span class="boring">use std::error::Error;
<pre><code class="language-rust ignore does_not_compile"><span class="boring">use std::error::Error;
</span><span class="boring">use std::fs;
</span><span class="boring">
</span><span class="boring">pub struct Config {
Expand All @@ -412,7 +407,7 @@ <h4 id="iterieren-über-zeilen-mit-der-methode-lines"><a class="header" href="#i
</span><span class="boring">}
</span><span class="boring">
</span><span class="boring">impl Config {
</span><span class="boring"> pub fn new(args: &amp;[String]) -&gt; Result&lt;Config, &amp;'static str&gt; {
</span><span class="boring"> pub fn build(args: &amp;[String]) -&gt; Result&lt;Config, &amp;'static str&gt; {
</span><span class="boring"> if args.len() &lt; 3 {
</span><span class="boring"> return Err(&quot;Nicht genügend Argumente&quot;);
</span><span class="boring"> }
Expand Down Expand Up @@ -450,8 +445,7 @@ <h4 id="iterieren-über-zeilen-mit-der-methode-lines"><a class="header" href="#i
</span><span class="boring">
</span><span class="boring"> assert_eq!(vec![&quot;sicher, schnell, produktiv.&quot;], search(query, contents));
</span><span class="boring"> }
</span><span class="boring">}
</span><span class="boring">}</span></code></pre></pre>
</span><span class="boring">}</span></code></pre>
<p><span class="caption">Codeblock 12-17: Iteriere über jede Zeile in
<code>contents</code></span></p>
<p>Die Methode <code>lines</code> gibt einen Iterator zurück. Wir werden in <a href="ch13-02-iterators.html">Kapitel
Expand All @@ -466,9 +460,7 @@ <h4 id="durchsuchen-aller-zeilen-nach-dem-abfragetext"><a class="header" href="#
in der Funktion <code>search</code> hinzu, wie in Codeblock 12-18 gezeigt. Beachte, dass
dies noch nicht kompiliert werden kann.</p>
<p><span class="filename">Dateiname: src/lib.rs</span></p>
<pre><pre class="playground"><code class="language-rust does_not_compile"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span><span class="boring">use std::error::Error;
<pre><code class="language-rust ignore does_not_compile"><span class="boring">use std::error::Error;
</span><span class="boring">use std::fs;
</span><span class="boring">
</span><span class="boring">pub struct Config {
Expand All @@ -477,7 +469,7 @@ <h4 id="durchsuchen-aller-zeilen-nach-dem-abfragetext"><a class="header" href="#
</span><span class="boring">}
</span><span class="boring">
</span><span class="boring">impl Config {
</span><span class="boring"> pub fn new(args: &amp;[String]) -&gt; Result&lt;Config, &amp;'static str&gt; {
</span><span class="boring"> pub fn build(args: &amp;[String]) -&gt; Result&lt;Config, &amp;'static str&gt; {
</span><span class="boring"> if args.len() &lt; 3 {
</span><span class="boring"> return Err(&quot;Nicht genügend Argumente&quot;);
</span><span class="boring"> }
Expand Down Expand Up @@ -517,8 +509,7 @@ <h4 id="durchsuchen-aller-zeilen-nach-dem-abfragetext"><a class="header" href="#
</span><span class="boring">
</span><span class="boring"> assert_eq!(vec![&quot;sicher, schnell, produktiv.&quot;], search(query, contents));
</span><span class="boring"> }
</span><span class="boring">}
</span><span class="boring">}</span></code></pre></pre>
</span><span class="boring">}</span></code></pre>
<p><span class="caption">Codeblock 12-18: Hinzufügen von Funktionalität, um zu
sehen, ob die Zeile die Zeichenkette in <code>query</code> enthält</span></p>
<p>Im Moment bauen wir die Funktionalität auf. Damit sie kompiliert werden kann,
Expand All @@ -531,9 +522,7 @@ <h4 id="speichern-passender-zeilen"><a class="header" href="#speichern-passender
<code>push</code>-Methode aufrufen, um eine <code>line</code> im Vektor zu speichern. Nach der
<code>for</code>-Schleife geben wir den Vektor zurück, wie in Codeblock 12-19 gezeigt.</p>
<p><span class="filename">Dateiname: src/lib.rs</span></p>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span><span class="boring">use std::error::Error;
<pre><code class="language-rust ignore"><span class="boring">use std::error::Error;
</span><span class="boring">use std::fs;
</span><span class="boring">
</span><span class="boring">pub struct Config {
Expand All @@ -542,7 +531,7 @@ <h4 id="speichern-passender-zeilen"><a class="header" href="#speichern-passender
</span><span class="boring">}
</span><span class="boring">
</span><span class="boring">impl Config {
</span><span class="boring"> pub fn new(args: &amp;[String]) -&gt; Result&lt;Config, &amp;'static str&gt; {
</span><span class="boring"> pub fn build(args: &amp;[String]) -&gt; Result&lt;Config, &amp;'static str&gt; {
</span><span class="boring"> if args.len() &lt; 3 {
</span><span class="boring"> return Err(&quot;Nicht genügend Argumente&quot;);
</span><span class="boring"> }
Expand Down Expand Up @@ -586,8 +575,7 @@ <h4 id="speichern-passender-zeilen"><a class="header" href="#speichern-passender
</span><span class="boring">
</span><span class="boring"> assert_eq!(vec![&quot;sicher, schnell, produktiv.&quot;], search(query, contents));
</span><span class="boring"> }
</span><span class="boring">}
</span><span class="boring">}</span></code></pre></pre>
</span><span class="boring">}</span></code></pre>
<p><span class="caption">Codeblock 12-19: Speichern der passenden Zeilen, damit
wir sie zurückgeben können</span></p>
<p>Jetzt sollte die Funktion <code>search</code> nur noch die Zeilen zurückgeben, die <code>query</code>
Expand Down Expand Up @@ -628,9 +616,7 @@ <h4 id="verwenden-der-funktion-search-in-der-funktion-run"><a class="header" hre
den Wert <code>contents</code>, den <code>run</code> aus der Datei liest, an die Funktion <code>search</code>
übergeben. Dann wird <code>run</code> jede von <code>search</code> zurückgegebene Zeile ausgeben:</p>
<p><span class="filename">Dateiname: src/lib.rs</span></p>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span><span class="boring">use std::error::Error;
<pre><code class="language-rust ignore"><span class="boring">use std::error::Error;
</span><span class="boring">use std::fs;
</span><span class="boring">
</span><span class="boring">pub struct Config {
Expand All @@ -639,7 +625,7 @@ <h4 id="verwenden-der-funktion-search-in-der-funktion-run"><a class="header" hre
</span><span class="boring">}
</span><span class="boring">
</span><span class="boring">impl Config {
</span><span class="boring"> pub fn new(args: &amp;[String]) -&gt; Result&lt;Config, &amp;'static str&gt; {
</span><span class="boring"> pub fn build(args: &amp;[String]) -&gt; Result&lt;Config, &amp;'static str&gt; {
</span><span class="boring"> if args.len() &lt; 3 {
</span><span class="boring"> return Err(&quot;Nicht genügend Argumente&quot;);
</span><span class="boring"> }
Expand Down Expand Up @@ -687,8 +673,7 @@ <h4 id="verwenden-der-funktion-search-in-der-funktion-run"><a class="header" hre
</span><span class="boring">
</span><span class="boring"> assert_eq!(vec![&quot;sicher, schnell, produktiv.&quot;], search(query, contents));
</span><span class="boring"> }
</span><span class="boring">}
</span><span class="boring">}</span></code></pre></pre>
</span><span class="boring">}</span></code></pre>
<p>Wir benutzen immer noch eine <code>for</code>-Schleife, um jede Zeile von <code>search</code>
zurückzugeben und auszugeben.</p>
<p>Jetzt sollte das gesamte Programm funktionieren! Lass es uns ausprobieren,
Expand Down
54 changes: 24 additions & 30 deletions ch12-05-working-with-environment-variables.html
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ <h3 id="schreiben-eines-fehlschlagenden-tests-für-die-suche-unabhängig-von-der
</span><span class="boring">}
</span><span class="boring">
</span><span class="boring">impl Config {
</span><span class="boring"> pub fn new(args: &amp;[String]) -&gt; Result&lt;Config, &amp;'static str&gt; {
</span><span class="boring"> pub fn build(args: &amp;[String]) -&gt; Result&lt;Config, &amp;'static str&gt; {
</span><span class="boring"> if args.len() &lt; 3 {
</span><span class="boring"> return Err(&quot;Nicht genügend Argumente&quot;);
</span><span class="boring"> }
Expand Down Expand Up @@ -294,7 +294,7 @@ <h3 id="implementieren-der-funktion-search_case_insensitive"><a class="header" h
</span><span class="boring">}
</span><span class="boring">
</span><span class="boring">impl Config {
</span><span class="boring"> pub fn new(args: &amp;[String]) -&gt; Result&lt;Config, &amp;'static str&gt; {
</span><span class="boring"> pub fn build(args: &amp;[String]) -&gt; Result&lt;Config, &amp;'static str&gt; {
</span><span class="boring"> if args.len() &lt; 3 {
</span><span class="boring"> return Err(&quot;Nicht genügend Argumente&quot;);
</span><span class="boring"> }
Expand Down Expand Up @@ -432,19 +432,17 @@ <h3 id="implementieren-der-funktion-search_case_insensitive"><a class="header" h
Hinzufügen dieses Feldes führt zu Kompilierfehlern, da wir dieses Feld noch
nirgendwo initialisiert haben:</p>
<p><span class="filename">Dateiname: src/lib.rs</span></p>
<pre><pre class="playground"><code class="language-rust does_not_compile"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span><span class="boring">use std::error::Error;
<pre><code class="language-rust ignore does_not_compile"><span class="boring">use std::error::Error;
</span><span class="boring">use std::fs;
</span><span class="boring">
</span>pub struct Config {
pub query: String,
pub file_path: String,
pub case_sensitive: bool,
pub ignore_case: bool,
}
<span class="boring">
</span><span class="boring">impl Config {
</span><span class="boring"> pub fn new(args: &amp;[String]) -&gt; Result&lt;Config, &amp;'static str&gt; {
</span><span class="boring"> pub fn build(args: &amp;[String]) -&gt; Result&lt;Config, &amp;'static str&gt; {
</span><span class="boring"> if args.len() &lt; 3 {
</span><span class="boring"> return Err(&quot;Nicht genügend Argumente&quot;);
</span><span class="boring"> }
Expand All @@ -459,10 +457,10 @@ <h3 id="implementieren-der-funktion-search_case_insensitive"><a class="header" h
</span><span class="boring">pub fn run(config: Config) -&gt; Result&lt;(), Box&lt;dyn Error&gt;&gt; {
</span><span class="boring"> let contents = fs::read_to_string(config.file_path)?;
</span><span class="boring">
</span><span class="boring"> let results = if config.case_sensitive {
</span><span class="boring"> search(&amp;config.query, &amp;contents)
</span><span class="boring"> } else {
</span><span class="boring"> let results = if config.ignore_case {
</span><span class="boring"> search_case_insensitive(&amp;config.query, &amp;contents)
</span><span class="boring"> } else {
</span><span class="boring"> search(&amp;config.query, &amp;contents)
</span><span class="boring"> };
</span><span class="boring">
</span><span class="boring"> for line in results {
Expand Down Expand Up @@ -530,27 +528,24 @@ <h3 id="implementieren-der-funktion-search_case_insensitive"><a class="header" h
</span><span class="boring"> search_case_insensitive(query, contents)
</span><span class="boring"> );
</span><span class="boring"> }
</span><span class="boring">}
</span><span class="boring">}</span></code></pre></pre>
</span><span class="boring">}</span></code></pre>
<p>Wir haben das Feld <code>ignore_case</code> hinzugefügt, das ein Boolean enthält. Als
Nächstes benötigen wir die Funktion <code>run</code>, um den Wert des Feldes <code>ignore_case</code>
auszuwerten, und verwenden diese, um zu entscheiden, ob die Funktion <code>search</code>
oder die Funktion <code>search_case_insensitive</code> aufgerufen werden soll, wie in
Codeblock 12-22 gezeigt. Dies kompiliert noch immer nicht.</p>
<p><span class="filename">Dateiname: src/lib.rs</span></p>
<pre><pre class="playground"><code class="language-rust does_not_compile"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span><span class="boring">use std::error::Error;
<pre><code class="language-rust ignore does_not_compile"><span class="boring">use std::error::Error;
</span><span class="boring">use std::fs;
</span><span class="boring">
</span><span class="boring">pub struct Config {
</span><span class="boring"> pub query: String,
</span><span class="boring"> pub file_path: String,
</span><span class="boring"> pub case_sensitive: bool,
</span><span class="boring"> pub ignore_case: bool,
</span><span class="boring">}
</span><span class="boring">
</span><span class="boring">impl Config {
</span><span class="boring"> pub fn new(args: &amp;[String]) -&gt; Result&lt;Config, &amp;'static str&gt; {
</span><span class="boring"> pub fn build(args: &amp;[String]) -&gt; Result&lt;Config, &amp;'static str&gt; {
</span><span class="boring"> if args.len() &lt; 3 {
</span><span class="boring"> return Err(&quot;Nicht genügend Argumente&quot;);
</span><span class="boring"> }
Expand All @@ -565,10 +560,10 @@ <h3 id="implementieren-der-funktion-search_case_insensitive"><a class="header" h
</span>pub fn run(config: Config) -&gt; Result&lt;(), Box&lt;dyn Error&gt;&gt; {
let contents = fs::read_to_string(config.file_path)?;

let results = if config.case_sensitive {
search(&amp;config.query, &amp;contents)
} else {
let results = if config.ignore_case {
search_case_insensitive(&amp;config.query, &amp;contents)
} else {
search(&amp;config.query, &amp;contents)
};

for line in results {
Expand Down Expand Up @@ -636,8 +631,7 @@ <h3 id="implementieren-der-funktion-search_case_insensitive"><a class="header" h
</span><span class="boring"> search_case_insensitive(query, contents)
</span><span class="boring"> );
</span><span class="boring"> }
</span><span class="boring">}
</span><span class="boring">}</span></code></pre></pre>
</span><span class="boring">}</span></code></pre>
<p><span class="caption">Codeblock 12-22: Aufruf von entweder <code>search</code> oder
<code>search_case_insensitive</code> basierend auf dem Wert in <code>config.ignore_case</code></span></p>
<p>Schließlich müssen wir nach der Umgebungsvariablen suchen. Die Funktionen zum
Expand All @@ -656,35 +650,35 @@ <h3 id="implementieren-der-funktion-search_case_insensitive"><a class="header" h
</span><span class="boring">pub struct Config {
</span><span class="boring"> pub query: String,
</span><span class="boring"> pub file_path: String,
</span><span class="boring"> pub case_sensitive: bool,
</span><span class="boring"> pub ignore_case: bool,
</span><span class="boring">}
</span><span class="boring">
</span>impl Config {
pub fn new(args: &amp;[String]) -&gt; Result&lt;Config, &amp;'static str&gt; {
pub fn build(args: &amp;[String]) -&gt; Result&lt;Config, &amp;'static str&gt; {
if args.len() &lt; 3 {
return Err(&quot;Nicht genügend Argumente&quot;);
}

let query = args[1].clone();
let file_path = args[2].clone();

let case_sensitive = env::var(&quot;IGNORE_CASE&quot;).is_err();
let ignore_case = env::var(&quot;IGNORE_CASE&quot;).is_ok();

Ok(Config {
query,
file_path,
case_sensitive,
ignore_case,
})
}
}
<span class="boring">
</span><span class="boring">pub fn run(config: Config) -&gt; Result&lt;(), Box&lt;dyn Error&gt;&gt; {
</span><span class="boring"> let contents = fs::read_to_string(config.file_path)?;
</span><span class="boring">
</span><span class="boring"> let results = if config.case_sensitive {
</span><span class="boring"> search(&amp;config.query, &amp;contents)
</span><span class="boring"> } else {
</span><span class="boring"> let results = if config.ignore_case {
</span><span class="boring"> search_case_insensitive(&amp;config.query, &amp;contents)
</span><span class="boring"> } else {
</span><span class="boring"> search(&amp;config.query, &amp;contents)
</span><span class="boring"> };
</span><span class="boring">
</span><span class="boring"> for line in results {
Expand Down Expand Up @@ -804,7 +798,7 @@ <h3 id="implementieren-der-funktion-search_case_insensitive"><a class="header" h
To tell your name the livelong day
To an admiring bog!
</code></pre>
<p>Ausgezeichnet, wir haben auch Zeilen mit „to“! Unser <code>minigrep</code>-Programm kann
<p>Ausgezeichnet, wir haben auch Zeilen mit „To“! Unser <code>minigrep</code>-Programm kann
jetzt ohne Berücksichtigung von Groß-/Kleinschreibung suchen, gesteuert durch
eine Umgebungsvariable. Jetzt weißt du, wie man Optionen verwaltet, die
entweder mit Kommandozeilenargumenten oder Umgebungsvariablen gesetzt werden.</p>
Expand Down
3 changes: 1 addition & 2 deletions ch12-06-writing-to-stderr-instead-of-stdout.html
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,13 @@ <h3 id="fehler-auf-der-standardfehlerausgabe-ausgeben"><a class="header" href="#
</span>fn main() {
let args: Vec&lt;String&gt; = env::args().collect();

let config = Config::new(&amp;args).unwrap_or_else(|err| {
let config = Config::build(&amp;args).unwrap_or_else(|err| {
eprintln!(&quot;Fehler beim Parsen der Argumente: {err}&quot;);
process::exit(1);
});

if let Err(e) = minigrep::run(config) {
eprintln!(&quot;Anwendungsfehler: {e}&quot;);

process::exit(1);
}
}</code></pre>
Expand Down
Loading

0 comments on commit 1c7598b

Please sign in to comment.