演習

  1. これらの名前は、何をすると示唆していますか?
  2. これらのシグネチャにはどのような名前を付けるべきですか?
// Copyright 2025 Google LLC
// SPDX-License-Identifier: Apache-2.0

// これらのメソッドの型は何ですか?
Option::is_some // ?
slice::get // ?
slice::get_unchecked_mut // ?
Option::as_ref // ?
str::from_utf8_unchecked_mut // ?
Rc::get_mut // ?
Vec::dedup_by_key // ?

// これらの型を持つメソッドにはどのような名前を付けるべきですか?
fn ____(String) -> Self;
fn ____(&self) -> Option<&InnerType>; // InnerType の詳細は重要ではありません。
fn ____(self, String) -> Self;
fn ____(&mut self) -> Option<&mut InnerType>;
  • 例のメソッドを順に見て、これらの関数の型がどうあるべきかを 議論してください。

  • 名前のないメソッドを順に見て、それらのメソッドにどのような名前を 付けるべきかを考えてください。

    不足している型の答え:

    • Option::is_some(&self) -> bool

    • slice::get(&self /* &[T] */, usize) -> Option<&T>

    • slice::get_unchecked_mut(&self /* &[T] */, usize) -> &T(unsafe かつ 簡略化したもの)

    • Option::as_ref(&self /* &Option<T> */) -> Option<&T>

    • str::from_utf8_unchecked_mut(v: &mut [u8]) -> &mut str(unsafe)

    • Rc::get_mut(&mut self /* &mut Rc<T> */) -> Option<&mut T>(簡略化したもの)

    • Vec::dedup_by_key<K: PartialEq>(&mut self /* &mut Vec<T> */, key: impl FnMut(&mut T) -> K) (簡略化したもの) 不足している名前の答え:

    • fn from_string(String) -> Self

    • fn inner(&self) -> Option<&InnerType> または as_ref(文脈による)

    • fn with_string(self, String) -> Self

    • fn inner_mut(&mut self) -> Option<&mut InnerType> または as_ref_mut (文脈による)