Build `rust_test` targets with `crate` using the same crate name as the underlying library target. (#1332)

This PR changes `rust_test` to use the same crate name as the underlying target, in cases where the `crate` attribute is used. This is consistent with `cargo`, which builds the crate using the name of the library when running `cargo test`.

The behavior of `rust_test` targets that don't use the `crate` attribute is unchanged.
diff --git a/docs/defs.md b/docs/defs.md
index 2e9bc76..342aed7 100644
--- a/docs/defs.md
+++ b/docs/defs.md
@@ -482,7 +482,9 @@
     deps = ["//some/dev/dep"],
 ```
 
-Run the test with `bazel build //hello_lib:hello_lib_test`.
+Run the test with `bazel test //hello_lib:hello_lib_test`. The crate
+will be built using the same crate name as the underlying ":hello_lib"
+crate.
 
 ### Example: `test` directory
 
@@ -533,7 +535,7 @@
 )
 ```
 
-Run the test with `bazel build //hello_lib:greeting_test`.
+Run the test with `bazel test //hello_lib:greeting_test`.
 
 **ATTRIBUTES**
 
diff --git a/docs/flatten.md b/docs/flatten.md
index c443f88..63eb09f 100644
--- a/docs/flatten.md
+++ b/docs/flatten.md
@@ -969,7 +969,9 @@
     deps = ["//some/dev/dep"],
 ```
 
-Run the test with `bazel build //hello_lib:hello_lib_test`.
+Run the test with `bazel test //hello_lib:hello_lib_test`. The crate
+will be built using the same crate name as the underlying ":hello_lib"
+crate.
 
 ### Example: `test` directory
 
@@ -1020,7 +1022,7 @@
 )
 ```
 
-Run the test with `bazel build //hello_lib:greeting_test`.
+Run the test with `bazel test //hello_lib:greeting_test`.
 
 **ATTRIBUTES**
 
diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl
index 532fe48..cc48b59 100644
--- a/rust/private/rust.bzl
+++ b/rust/private/rust.bzl
@@ -345,7 +345,6 @@
     _assert_no_deprecated_attributes(ctx)
     _assert_correct_dep_mapping(ctx)
 
-    crate_name = compute_crate_name(ctx.workspace_name, ctx.label, toolchain, ctx.attr.crate_name)
     crate_type = "bin"
 
     deps = transform_deps(ctx.attr.deps)
@@ -363,7 +362,7 @@
 
         # Build the test binary using the dependency's srcs.
         crate_info = rust_common.create_crate_info(
-            name = crate_name,
+            name = crate.name,
             type = crate_type,
             root = crate.root,
             srcs = depset(ctx.files.srcs, transitive = [crate.srcs]),
@@ -381,7 +380,7 @@
     else:
         # Target is a standalone crate. Build the test binary as its own crate.
         crate_info = rust_common.create_crate_info(
-            name = crate_name,
+            name = compute_crate_name(ctx.workspace_name, ctx.label, toolchain, ctx.attr.crate_name),
             type = crate_type,
             root = crate_root_src(ctx.attr, ctx.files.srcs, "lib"),
             srcs = depset(ctx.files.srcs),
@@ -1063,7 +1062,9 @@
             deps = ["//some/dev/dep"],
         ```
 
-        Run the test with `bazel build //hello_lib:hello_lib_test`.
+        Run the test with `bazel test //hello_lib:hello_lib_test`. The crate
+        will be built using the same crate name as the underlying ":hello_lib"
+        crate.
 
         ### Example: `test` directory
 
@@ -1117,7 +1118,7 @@
         )
         ```
 
-        Run the test with `bazel build //hello_lib:greeting_test`.
+        Run the test with `bazel test //hello_lib:greeting_test`.
 """),
 )
 
diff --git a/test/rust/BUILD.bazel b/test/rust/BUILD.bazel
index c9bc603..1977aed 100644
--- a/test/rust/BUILD.bazel
+++ b/test/rust/BUILD.bazel
@@ -1,4 +1,4 @@
-load("//rust:defs.bzl", "rust_binary", "rust_library")
+load("//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")
 
 package(default_visibility = ["//visibility:public"])
 
@@ -17,3 +17,8 @@
     edition = "2018",
     deps = [":hello_lib"],
 )
+
+rust_test(
+    name = "hello_lib_test",
+    crate = ":hello_lib",
+)
diff --git a/test/rust/src/lib.rs b/test/rust/src/lib.rs
index 9dc6089..bee47c9 100644
--- a/test/rust/src/lib.rs
+++ b/test/rust/src/lib.rs
@@ -12,4 +12,5 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+#![crate_name = "hello_lib"]
 pub mod greeter;