Skip to content

Commit

Permalink
Merge pull request #93 from woodyZootopia/update-10-03
Browse files Browse the repository at this point in the history
Update 10-03
  • Loading branch information
tatsuya6502 authored Mar 12, 2022
2 parents 04aefa1 + fe5a57e commit fea0ef1
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 264 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
$ cargo run
Compiling chapter10 v0.1.0 (file:///projects/chapter10)
error[E0597]: `x` does not live long enough
(エラー[E0597]: `x`の生存期間が短すぎます)
--> src/main.rs:7:17
|
7 | r = &x;
| ^^ borrowed value does not live long enough
| (借用された値の生存期間が短すぎます)
8 | }
| - `x` dropped here while still borrowed
| (`x`は借用されている間にここでドロップされました)
9 |
10 | println!("r: {}", r);
| - borrow later used here
| (その後、借用はここで使われています)

error: aborting due to previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ fn main() {
let string2 = "xyz";

let result = longest(string1.as_str(), string2);
// 最長の文字列は、{}です
println!("The longest string is {}", result);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
$ cargo run
Compiling chapter10 v0.1.0 (file:///projects/chapter10)
error[E0106]: missing lifetime specifier
(エラー[E0106]: ライフタイム指定子が不足しています)
--> src/main.rs:9:33
|
9 | fn longest(x: &str, y: &str) -> &str {
| ^ expected lifetime parameter
| (ライフタイム引数があるべきです)
|
= help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `x` or `y`
(助言: この関数の戻り値型は借用された値を含んでいますが、
シグニチャは、それが`x`と`y`どちらから借用されたものなのか宣言していません)

error: aborting due to previous error

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// ANCHOR: here
fn main() {
// 長い文字列は長い
let string1 = String::from("long string is long");
// (訳注:この言葉自体に深い意味はない。下の"xyz"より長いということだけが重要)

{
let string2 = String::from("xyz");
let result = longest(string1.as_str(), string2.as_str());
// 一番長い文字列は{}
println!("The longest string is {}", result);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ struct ImportantExcerpt<'a> {
}

fn main() {
// 僕をイシュマエルとお呼び。何年か前・・・
let novel = String::from("Call me Ishmael. Some years ago...");
// "'.'が見つかりませんでした"
let first_sentence = novel.split('.').next().expect("Could not find a '.'");
let i = ImportantExcerpt {
part: first_sentence,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
$ cargo run
Compiling chapter10 v0.1.0 (file:///projects/chapter10)
error[E0515]: cannot return value referencing local variable `result`
(エラー[E0515]: ローカル変数`result`を参照している値は返せません)
--> src/main.rs:11:5
|
11 | result.as_str()
| ------^^^^^^^^^
| |
| returns a value referencing data owned by the current function
| `result` is borrowed here
| (現在の関数に所有されているデータを参照する値を返しています
| `result`はここで借用されています)

error: aborting due to previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ fn main() {

// ANCHOR: here
fn longest<'a>(x: &str, y: &str) -> &'a str {
// 本当に長い文字列
let result = String::from("really long string");
result.as_str()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ impl<'a> ImportantExcerpt<'a> {
// ANCHOR: 3rd
impl<'a> ImportantExcerpt<'a> {
fn announce_and_return_part(&self, announcement: &str) -> &str {
// "お知らせします: {}"
println!("Attention please: {}", announcement);
self.part
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fn longest_with_an_announcement<'a, T>(
where
T: Display,
{
// "アナウンス! {}"
println!("Announcement! {}", ann);
if x.len() > y.len() {
x
Expand Down
Loading

0 comments on commit fea0ef1

Please sign in to comment.